tags:

views:

69

answers:

3

why cant we use non static data members declared outside a static method within a static method?

+4  A: 

Non static members belong to an object. A static method has no object.

If we have

class MyClass {
int member;
. . .

public static int statFunc() {
   . . .
   foo = member;
   . . .
}
. . .

}

If we have two instances of MyClass one where member = 1 and another where member = 2 and we call statFunc then statFunc has no idea which value of member to use.

deinst
@deinst: thanks
brainless
+1  A: 

Try this, it seems you're confused by static keyword:
http://download-llnw.oracle.com/javase/tutorial/java/javaOO/classvars.html

Nikita Rybak
A: 

Because it doesn't make any sense. Instance variables are associated with an instance of the class. Static methods aren't. Which instance's variables would you be talking about within the static method?

EJP
Does somebody disagree?
EJP