Static variables are one per class Instance variables are one per instance (each object has its own copy) Static methods/variables are on the stack, while objects and all their data (including their instance variables) are on the heap. From what I understand, the stack is a set amount of memory given to a particular program. So when you start the program, the stack is a preset size, depending on the program, and it does not grow or shrink. The heap on the other hand is any free memory on your system. When you call the 'new' operator, the program requests some amount of memory from the system's free RAM. This whole concept is based on your entire system, but in the case of Java, the Virtual Machine is the system. As far as instance methods, I don't know if they are copied for each object, or if there is one copy somewhere on the heap. I'm sure someone else knows. If I had to guess, I'd say it's copied per instance in Java. But that's purely an uneducated guess.
There is more detail about how the memory is allocated in the JVM in this page from the Jakarta BCEL project. Much more detail, which requires very careful reading! If I remember correctly, it tells us there is one copy of each method for each class. Also there is one copy of each static variable for each class, which can be shared between all the instances. The whole lot all together is called a "class object."
There are as many instances in the "heap" as you can create and fit in. Each instance on the heap has details of its fields, but does not contain its methods. Rather, it has a reference back to its own class object, where the methods are. Whenever you call a method on an object, the details are passed back to the class object, along with a reference to the instance on the heap.
They explain it a lot better in BCEL!
Joined: Oct 13, 2005
I think I have given the impression that static variables are in the same part of memory as methods. Not what I intended to mean. Not sure whether it is true or false.
Thanks Ritchie, the article suggested by you is very informative. And I still wonder why haven't you been promoted to bartender after so much participation. Hope this goes to the senior Java Ranchers..
Joined: Apr 22, 2007
thanks for your valuable information on stack ....
but i still have small doubt... can any one explain clearly what this means.... "The methods of an object defines its behavior and are called instance methods.It is important to note that these methods pertain to each object of class.This should not be confused with the implementation of the methods, which is shared by all instances of the class."
They know what an awkward so-and-so I would be; if I were bartender nobody would ever get a drink .
About that quote from Mughal: I can make various interpretations of it.
Get a different book which you can actually understand.
Go onto the part of the Ranch called Bunkhouse Books and write a review of that book. What is the use of a book if you can't understand it?
Maybe . . . Therre is one copy of each method which is shared by every instance, but each instance produces different results and effects because its fields have different contents and values.
Maybe . . .
Joined: Apr 22, 2007
thanks Campbell for your clarification
i am following head first and Java Tutorial(from sun). i am planning for Java Certification. First may be it will take some time for me getting the fundamentals clear. i am new to this forum.hope i can get some good assistance from you all.