aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class - Static variables Questions? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Class - Static variables Questions? " Watch "Class - Static variables Questions? " New topic
Author

Class - Static variables Questions?

satya mamillapalli
Ranch Hand

Joined: Jun 22, 2005
Posts: 63
"Static variables are created when class is loaded at runtime and exists as long as the class exists."

Can someone explain this about class variables..

When a class is loaded at runtime <- Does this mean when an object is created first time ?

Where do the class resides? heap?

How long does the class exits <- as long as JVM running?


Please Advise..

Thanks
Satya
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
Originally posted by satya mamillapalli:

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


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
nagaraj raja
Ranch Hand

Joined: Aug 06, 2005
Posts: 36
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
nagaraj raja
Ranch Hand

Joined: Aug 06, 2005
Posts: 36
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
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
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".
satya mamillapalli
Ranch Hand

Joined: Jun 22, 2005
Posts: 63
Thnaks for your replies..

Where do the class resides? heap?

Do they ever eligible for garbage collection?


How long does the class exits <- as long as JVM running?

Will ever be a class get deleted from the heap due to memory size(heap size)?


Please explain.

Thanks
Satya
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Loading means converting the source code into ByteCode i.e.. converting the .java file into .class file.



I think that this is called compilation.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724

How long does the class exits <- as long as JVM running?


I suppose the class will exists as long as the CLASSLOADER which loads this class will exists itself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class - Static variables Questions?