Sponsored Link •
|
Advertisement
|
Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
Message:
Static methods cannot access non-static memeber variables because non-static memeber variables ( also called instance variables ) are created for each object ( also call instance ) of a class. So if you don't have any object of a class then there is no copy of instance variables. However, static member variables ( also called class memeber variables ) can exist without any object of that class. In fact they belong to class not to any particular object of that class. Since you can call static method of a class just using class name ( without creating object ) which non-static variable will it refer to because there may be any no. of non-static variables lying in the memory ( one for each object of that class ). Again non-static methods can refer to instance variables and when you call a non-static method java automatically referas to the copy of instance variable ( i mean which object's instance variable to refer to ) for the object you call the method for. The same problem would occur if you call non-static methods from static method. If we call a non-static method ( which refers to non-static member variables inside it ) from a static method then which copy of memeber variable the code in non-static method will refer to( because there is one copy non-static memeber variables for each object ).
Replies:
|
Sponsored Links
|