I understand that an object and its instance variables reside on heap, when the object is created.
Each object will have its own copy of instance variables.
Where and when does a static variable reside. If I am right, it does need an object to be created. Also, when two objects of the class are created, boh objects share the same copy of the static variable. Hence, the static variable is not living inside the object, then where is it?
Thanks and Regards,
Originally posted by Javed Mohammed:
Where and when does a static variable reside.
A static variable comes into existence when the class is loaded.
If I am right, it does need an object to be created.
No, a static variable does not need to have any objects created. Any statement that references a class will cause it to be loaded (and therefore init its static members) if it is not already.
or even just
Like I said, I am not sure if the static variables are associated with this class or not. You may want to check out the Java Language Specification (JLS) to see what it says.