aspose file tools*
The moose likes Beginning Java and the fly likes Static is painful! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static is painful!" Watch "Static is painful!" New topic
Author

Static is painful!

Madhusudan Banik
Greenhorn

Joined: Jan 14, 2007
Posts: 10
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?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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 ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Madhusudan Banik
Greenhorn

Joined: Jan 14, 2007
Posts: 10
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

Joined: Oct 30, 2001
Posts: 1970
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Madhusudan Banik
Greenhorn

Joined: Jan 14, 2007
Posts: 10
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static is painful!