• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class - Static variables Questions?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic