Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes dif. between class and instance data members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dif. between class and instance data members" Watch "dif. between class and instance data members" New topic
Author

dif. between class and instance data members

Margaret Tan
Greenhorn

Joined: Sep 14, 2001
Posts: 20
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

Joined: Mar 16, 2001
Posts: 984


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?

Please ignore post, I have no idea what I am talking about.
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
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


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dif. between class and instance data members
 
Similar Threads
Primitive variable
When will a variable use memory from stack and when will use memory from heap?
Object and Varaible
Memory allocation for interface
Memory accocation in case of arrays