Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java:57: non-static variable this cannot be referenced...

Status
Not open for further replies.

tsp1lrk

IS-IT--Management
May 30, 2001
103
US
Hi, I'm struggling a bit with a program I'm trying to write-- I keep getting the famous:

java:57: non-static variable this cannot be referenced from a static context

Newbie to Java-- I can't seem to figure out what's wrong:

import java.io.*;
import java.text.*;

// Name of Program.
class storm {
public class LK_Employee2 {


int num = 0;
// instance variable

void ask() {
// instance method
// Accept user input.

System.out.print("Please enter an Employee ID:");
// body of method

}


void get_disp() {

// instance method
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
String myString = br.readLine();

num = Integer.parseInt(myString);
// body of method
}

}

// Defines the start of a method called main.

public static void main (String args[]) throws Exception {


LK_Employee2 hazelnut= new LK_Employee2();
hazelnut.ask();
hazelnut.get_disp();

System.out.println("The Employee ID you entered was: " +hazelnut.num);



// Exit the program.

System.exit(0);

}
}

Thanks-
Lisa
UPS
 
Well you are trying to create an instance of an inner class of class "storm" from outside that class scope.
You need to create a new "storm" class, and then invoke your inner class from some method.

Your code also had a few naming conventions which are not considered good practice, and also it was missing some exceptions. I have modified accordingly for you :)

Code:
import java.io.*;

  class Storm {

	public void doIt() {
		StormInner hazelnut = new StormInner();
		hazelnut.requestInput();
		hazelnut.getInput();

		System.out.println("The Employee ID you entered was: " +hazelnut.getResponse());
	}


	// Defines the start of a method called main.
    public static void main (String args[]) throws Exception {
		Storm s = new Storm();
		s.doIt();

  	}

	class StormInner {
		private int num = 0;

		int getResponse() {
			return num;
		}

		void requestInput() {
			// instance method
			// Accept user input.

			System.out.print("Please enter an Employee ID:");
			// body of method

		}

		void getInput() {
			// instance method
			String myString = "";
			try {
				BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
				myString = br.readLine();
				// body of method
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}

			try {
				num = Integer.parseInt(myString);
			} catch (NumberFormatException nfe) {
				System.out.println("Not a number entered !");
				requestInput();
				getInput();
			}
		}

	}

}

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks I appreciate it- I have to make sure that I show the following in my program:

• one class
• at least one instance variable (the number) and two instance methods, (ask for and get the ID from the user and display it), both of which are used in main
• at least one local variable
• no static variables or static methods other than main,
• user input from the keyboard to provide the initial number. See the Io.java program.

Is this correct?

Thanks-
Lisa
 
I work full time and utilize VB.net so this is a bit foreign to me; I need to utilize Java to create something at work that my boss and I are testing-- he's trying help me out with java and gave this to me-- he took classes on this years ago, so now he's trying to assist me in it. He knows I'm here. ;>) I just need to know were I went wrong in my PROGRAM; I don't want the answer, just some guidance on why it's wrong. Thanks-- I know this forum is NOT for STUDENTS getting answers--
 
OK, just had to check about the student thing :)

• one class
Well, no because you have two classes (the main outer class and your inner class). Just forget about the inner class and move those methods into your outer main class.

• at least one instance variable (the number) and two instance methods, (ask for and get the ID from the user and display it), both of which are used in main
Yes, num is an instance variable.

• at least one local variable
Yes, "hazlenut" is a local variable.

• no static variables or static methods other than main,
Yes, you have this.

• user input from the keyboard to provide the initial number. See the Io.java program.
Yes, you have this.

--------------------------------------------------
Free Database Connection Pooling Software
 
Hi-- Thanks for the advice; Just an FYI... I think it's great that you ask first about the students. There is no way one can learn by cheating, that's for sure; so I appreciate you checking with me first, that shows me that this forum is really a great and serious resource.

Thanks again-

Regards,
Lisa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top