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:
some points about static
Posted by Jeganathan S on October 31, 2000 at 12:25 AM
Hi Arulmozhi, I don't know whether this will clear ur doubt as u have not given ur question clearly, k let me try. Actually if u declare a variable in class, those variables will be alloted with memory when they r instantiated and seperate variables will be given 4 each and every object u instantiate. What i mean by this is nothing but even if u change the value of a variable say x in one object will not affect the value of x in other objects,but it is not the case if u declare a variable as static, a static variable can also be called as a class variable as it is only one variable which is going 2 b shared by as many object u instantiated from that class, so if u change the value of static variable in any object will be reflected in other objects instantiated from the same class, i hope u got it. Moreover, if u have a method declared as static then that method can use only static variables, that is y when u try using non- static variables within public static void main(String args[])reports some error. Also u have a static block which will be executed only once though u create many objects from the same class unlike constructor which will be called 4 every object u create from the class.
Replies:
|