Help coderanch get a
new server
by contributing to the fundraiser
  • 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

question about static variable's residence

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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,

Javed.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric is correct, if he means that the static variables don't need an object of the class that contains them. However, there IS an object created when a class is loaded. The object is of type Class and represents the class. I don't know if static variables are associated with this object or not. This is one nice thing about Java: general you don't have to worry about these details. Of course, it is sometimes interesting to learn about just as an academic pursuit.

Layne
 
Javed Mohammed
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OOPS.......I did mean to say that static varibales does not need an object, that was a typing mistake.

"An object is created when class is loaded", this answers my queston.

Thanks everybody.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to clarify, let's say you have a class named MyClass. The object created that I mentioned is NOT an object of type MyClass. It is an object created from java.lang.Class. This object represents the MyClass type.

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.

Layne
 
Whatever you say buddy! And I believe this tiny ad too:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic