Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables

 
Swati sb
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Want to know where static variables are stored when a class is loaded.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mostafa Radwan
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The explanation by Mike is correct and in accordance with the JLS 2e 8.3.1.1.
No concepts were "violated".
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim:

Thanks for spotting the confusion. We could have gone back and forth for a week without you.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic