• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

dif. between class and instance data members

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic