This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dif. between class and instance data members

 
Margaret Tan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell me the difference between these two besides that we don't need to create an instance of a class to access a class data member but vice versa for instance data members? And how they are allocated in memory? For instance, there is an object reference in the stack memory that points to the object in the heap memory that contains data members for this particular object. How about class data members? Where are they allocated? In the stack memory or in the heap memory. Thanks a lot!
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

don't need to create an instance of a class to access a class data member

Really? How can you do anything with a class if you don't have an instance of it?
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think she's referring to the static class members which can be used through a reference to the class name. No object need be created to do that. As far as the memory allocation differences, I'm not quite sure, I'll pass that one on to a higher power.....Cindy?
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static references only require that the class be loaded, not that an instance of the class be created. When a class is loaded the static variables are set up in the ClassFile area and the methods are loaded into the "Method Area" which is a secured area that you and I can not mess with.
This allows the use of such things as "Math.PI" and "double sqrt = Math.sqrt(someNumber);" without creating any little Maths to do the job.
The location of various items is all described in the JVM Specification. Mostly in 3.5 Runtime Data Areas. There is some leniancy given to how a specific vendor may choose to implement the specifications of a JVM.

Although the method area is logically part of the garbage-collected heap, simple implementations may choose to neither garbage collect nor compact it.

Also you can see the actual format of a Class File.

4.1 ClassFile
A class file contains a single ClassFile structure:
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info constant_pool[constant_pool_count-1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count]; // These are the static fields
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}

Of course the instance field values are kept with the object which is stored on the garbage collectable heap.
[This message has been edited by Cindy Glass (edited October 28, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic