When a class is loaded at runtime <- Does this mean when an object is created first time ?
Well, the definition of static variables states that they are class-wide variables. They do not require an object to be initialized as every object has it's own separate copy of variables. If we use an object, we will have multiple copies of the static variable which contradicts the definition of static variables.
So, static variables are initialized only once when the class is loaded.
Loading a class does not mean instantiating it with an object. Loading means converting the source code into ByteCode i.e.. converting the .java file into .class file.
Where do the class resides? heap?
Yes the class resides in the JVM heap.
How long does the class exits <- as long as JVM running?
Even I'm not sure about this. I think as long as there are other references pointing to the class
i have dout regarding this ok according to ur openion static variable are loaded at runtime if so why static methods cont be overidden cant apply to runtime polymorphism so i am thinking static variables are loaded at runtime only what's ur openion about this nag
i have dout regarding this ok according to ur openion static variable are loaded at runtime if so why static methods cont be overidden cant apply to runtime polymorphism so i am thinking static variables are loaded at runtime only what's ur openion about this nag
Sherry is right ... static variables are created when the class is loaded.
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".