0

I'm trying to pick up Java again using the book Data Structures & Algorithms in Java, in pg 191, the book implements link list. The code first builds a link class, a link list class and a client class linkapp.

public class Link {
    public int data;
    public link next;

    public Link (int newdata){
        data=newdata;
    }   
}
public class Linklist {
    public Link first;

    public void insertFirst(int data){
        Link  newlink=new Link  (data);
        newlink.next=first;
        first=newlink;
    }

    public boolean isEmpty(){   return (first==null); }

    public void displaylist(){
        if (!isEmpty()){
            Link current=first;
            while(current!=null){
                System.out.print(current.data);
                current=current.next;}
        }
    }
}
public class LinklistApp {
    public static void main(String[] args) {
        Linkedlist linkList = new Linkedlist();
        linkList.insertFirst(3);
        linkList.insertFirst(4);
        linkList.insertFirst(5);
        linkList.displaylist();
    }
}

But I don't understand why the link object created locally in the insertFirst method inside linklist class can be accessed by displaylist method. Local variables will disappear when the method exits because they're just intermediate result. So how did displaylist method still have access to those link objects?

2 Answers 2

3

The value of the newlink variable in insertFirst is copied into the instance variable here:

first=newlink;

Then in displayLink, the value of first is copied back into a local variable:

link current=first;

It's important to distinguish between variables and their values - and also between references and objects. The values of all these link variables are only references.

(As an aside, is this example straight out of the book? If so, I'm concerned at the use of unconventional names like link instead of Link for a class, and newlink instead of newLink as a variable.)

Sign up to request clarification or add additional context in comments.

4 Comments

Could you care to elaborate more? How did I mix up variable and its value or object and its reference?
@GorillaInR: Well you're talking about the local variables "disappearing" but you're surprised that the link object created in insertFirst is available later. Quite simply, a reference to that object was stored in an instance variable. Just because the local variable goes out of scope doesn't mean anything happens to the object that its value happens to refer to.
more specifically does it mean that because first is an instance variable, and the last inserted link object is stored in first, and second last inserted link object is stored in first's next field/link, and the third last inserted link object is stored in first's next link's next link, and so on? So these link objects are accessible because they're stored in the first's layers of next fields and not because newlink still exists?
@GorillaInR: The objects aren't stored in fields - references are. Again, it's important to keep that distinction clear in your head. But yes, each object refers to the next.
0

Objects that are created using the new keyword are allocated on the Heap and then the address for that object is pushed onto the local call stack.

When the local Stack Frame is popped off the call stack, the object pointer (the local variable) goes out of scope. This temporarially orphans the object on the Heap. This is where Garbage Collection comes in

The Garbage Collector (GC) will crawl the Heap looking for object who's addresses aren't referenced anywhere else in the call stack and then dispose of them.

In the case of the link object that is created locally in the insertFirst method, that address is still referenced by an object on the call stack, so the GC never collects that object, and so the object persists. When you execute the line first=newlink you are storing the address to that object in a place that does not go out of scope until the parent object is collected.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.