File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dif. between class and instance data members" Watch "dif. between class and instance data members" New topic

dif. between class and instance data members

Margaret Tan

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: 986

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"

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:
subject: dif. between class and instance data members
It's not a secret anymore!