• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class - Static variables Questions?

 
satya mamillapalli
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
nagaraj raja
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sherry is right ... static variables are created when the class is loaded.
 
satya mamillapalli
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loading means converting the source code into ByteCode i.e.. converting the .java file into .class file.



I think that this is called compilation.
 
David Ulicny
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic