Stack and heap

If a Label object is created in an init() method, where is the pointer variable to the object? Is the name of the Label object put on the stack, and a pointer to the heap when the instance of the Label is created with new? Or is the pointer inside the instance of the applet already on the heap?

My question is: are all object names put on the stack, no matter where they are declared and created?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I'm guessing that you did something like the following in your init:

add(new Label("Header"));

and are now wondering how come the label persists, since there are no apparent references to it. And, anything without references to it gets garbage collected!

However, if this is the case you are referring to, contrary to appearances there are explicit references to this object. When the applet is fired up the Applet object itself is created, kind of like a "root" object. An applet is a derivative of a Panel object, and a Panel maintains lists of the Components being displayed on it. So, there will in fact be a reference to the Label you created, in the AWT datastructures.

So the Label object will be on the heap (where all objects are) and the pointer to it will be part of another class, which is also on the heap. The stack is only used to pass arguments, and transfer and return control to and from methods.
HanRuiAuthor Commented:
The last paragraph is very close to what I am looking for. when I took Java at Sun microsystems, the notes indicate that names of object instances are on the stack, and the object is on the heap. The problem I have is interpreting the location of a pointer for an object that is created by an object on the heap. A strict interpretation of the Sun notes is that the object name is put on the stack, with a pointer to the heap. Your interpretation is closer to mine -- that the pointer is in the former object, which is on the heap. This means that pointers are on the heap and we can have a chain of pointers across objects.

According to my Sun notes, when an object is declared, the name goes on the stack, and the accompanying pointer field is undefined. then when the object is created, the pointer field takes on a value, pointing to the object on the heap.
In your program you only have direct access to the stack.  Any variable declared inside a method is on the stack frame for that method.  These variables typically hold references (or pointers, whatever, i think reference works better as an analogy to C/C++) to things on the Heap, which is anything declared with new.  For example the statement new myClass(); puts an instance of myClass, with all of its fields, on the heap.  The statement myClass x = new myClass(); also adds a reference on the stack called x that refers to this stuff on the heap.
HanRuiAuthor Commented:
This is closer to what I need to understand the behavior of the stack and heap. I understand the variables that are declared inside a method. What about declarations of class instance variables. My notes say the these declarations go on the stack first, and when the instance is created, the instance goes on the heap. Do I understand,then, that the fields that may be stored within an instance on the heap would never be pointers to another object on the heap? I understand that we really don't manipulate "pointers" in Java, but they are there anyway.
no, fields stored on the heap often refer to objects on the heap
for example
public class Class1{
     private Class2 InstanceVariable1;
     public Class1(){
         InstanceVariable1 = new Class2();
Class1's constructor makes a new instance of Class2 (which resides on the heap) and assigns a reference to this class to an instance variable (also on the heap!)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.