aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static variables" Watch "Static variables" New topic
Author

Static variables

Swati sb
Greenhorn

Joined: Feb 13, 2005
Posts: 2
Want to know where static variables are stored when a class is loaded.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The first time your program refers to a class, a class object is created on the heap. static variables are part of the class object, just as instance variables are part of the class's instances.


Mike Gershman
SCJP 1.4, SCWCD in process
Mostafa Radwan
Greenhorn

Joined: Sep 09, 2003
Posts: 25
Hi Swati ...
I'd prefer you check out the Java Language Specefication(JLS) beacuase I'm not sure of Mike's explanation that static variables are part of the class object (this violates the concept of static members at all).


- Mostafa Radwan -
M.Sc | SCJP 1.4
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
The explanation by Mike is correct and in accordance with the JLS 2e 8.3.1.1.
No concepts were "violated".


Tony Morris
Java Q&A (FAQ, Trivia)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
this violates the concept of static members

What concept were you referring to?

Where do you think static members are stored? They can't be on the stack because static members survive after the method that first referenced the class has returned.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Mostafa: perhaps it would help if Mike had said "a Class object" instead of "a class object". He's not talking about an instance of the class that is declaring the static variable (let's call it "MyClass") - he's talking about an instance of the class Class, an instance which represents MyClass. When MyClass is loaded, a single Class object is created, called MyClass.class. This is where static fields are stored.
[ February 13, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Jim:

Thanks for spotting the confusion. We could have gone back and forth for a week without you.
 
Don't get me started about those stupid light bulbs.
 
subject: Static variables