• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

Static is painful!

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oject are created in heap at runtime, local field are created when the oject are created in runtime stack of the current thread. Now my question is when static field, static method, static nested class created (come in to existance / probaly when the class is load) and where they are placed in memory? Are they in runtime stack or heap or any thing else?
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All objects, whether referred to by static fields, instance fields or local variables, are stored on the heap.

Remember that Java fields are not objects, but they can be references to objects. (The alternative is that they are primitives, like int).

Instance fields are part of the object instance on the heap. Static fields are (conceptually at least) part of the class, as loaded by a particular ClassLoader.

In simple applications, you only have the system ClassLoader, so each static field has one value in the whole JVM. In complex applications (e.g. Web application servers), the same class may be loaded by more than one ClassLoader. In that case, each static field has one value for each ClassLoader. If you're a beginner, don't worry overly about this, but revisit it when you understand class loading fully.
[ February 05, 2007: Message edited by: Peter Chase ]
 
Madhusudan Banik
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
static int i;
static int j;
static void f();

}

Now when the class A is loaded by a classloader, what its do when it see static int i or static void f(). I think that when a object of class A is created there is no space reserved for the static field i in the heap but for j. And also what is the difference between non static field and instance field. Thanks for reply.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A "non-static field" is just another term for an instance field.

No additional space is reserved per instance for static fields. The space for static fields is associated with the loaded class.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The bytecode for methods (no matter whether static or not) is stored in a memory space called the "permanent generation". Note that instance methods are *not* duplicated for each instance of the class.

I'm not totally sure, but I'd assume that static fields are stored in the permanent generation, too.
 
Madhusudan Banik
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, for reply. Last of all, is parmanent generation is populated only when the class is loaded or in different phase in the program execution? And why an instance method may define a final static field but could not define a static variable?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rony Banik:
And why an instance method may define a final static field but could not define a static variable?



An instance method *cannot* define a field, whether final static or not.

Are you referring to anonymous inner classes, perhaps?
 
reply
    Bookmark Topic Watch Topic
  • New Topic