-1

I'm new to Java and I found this linked list implementation down below. In the main method we create a LinkList instance named theLinkedList and using that we call insertFirstLink method 4 times. What InsertFirstLink method does is it creates a Link instance named newLink. When we call insertFirstLink 4 times.

Does this method creates 4 Link instances with the same name (newLink)? How is that possible? We can't create objects with the same name right? What am I missing?What do I need to study to understand this part?

Thank you guys. I understood my problem. After every execution the new link variable is destroyed, but every variable destroyed has a reference and its like a line. We can always go through the line and find the node we want.

public class Link {

    public String bookName;
    public int millionsSold;
    public Link next;

    public Link(String bookName, int millionsSold) {
        this.bookName = bookName;
        this.millionsSold = millionsSold;
    }

    public static void main(String[] args) {
        LinkList theLinkedList = new LinkList();
        theLinkedList.insertFirstLink("Don Quixote",500);
        theLinkedList.insertFirstLink("A Tale of two cities",200);
        theLinkedList.insertFirstLink("The Lord Of The Rings",150);
        theLinkedList.insertFirstLink("Harry Potter",1000);
    }
}

class LinkList {

    public Link firstLink;

    LinkList() {
        firstLink = null;
    }

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

    public void insertFirstLink(String bookName, int millionsSold) {
        Link newLink = new Link(bookName, millionsSold);
        newLink.next = firstLink;
        firstLink = newLink;
    }

}
  • 3
    Variables' (not objects, they don't have names) names need to be unique in *scope*. – Kayaman Sep 01 '17 at 13:08
  • List nodes do not have names. `newLink` is a **local** reference to a newly created object. I suggest that you review variable *types* and *scopes*. – PM 77-1 Sep 01 '17 at 13:10
  • I thought that instance is an object. But without a name how the compiler recognizes it. Can you give me more details. – Kusal Dananjaya Sep 01 '17 at 13:14
  • Thanks for the reply. I will review more on that – Kusal Dananjaya Sep 01 '17 at 13:16
  • `newLink` actually isn't an object per se. It is a variable that holds a *reference* to an object. That variable can point to an object X at a certain moment, and later to an object Y. It can even point to no object at all, that's when the variable is said to hold a *null* reference. – Milack27 Sep 01 '17 at 13:17
  • Just to add for clarity, check out this small code - `while(currentLink != null) { currentLink.printLink(); currentLink = currentLink.nextLink; }` Now you see that the variable (handle to that data that you added to the list) was destroyed. So you can't really get the data f. e. on 5 place in the LinkedList if you don't iterate through them. – Guillotine Sep 01 '17 at 13:18

4 Answers4

1
public void insertFirstLink(String bookName, int millionsSold) {
     Link newLink = new Link(bookName, millionsSold);
     newLink.next = firstLink;
     firstLink = newLink;
}

This method don't create the same variable with the same name 4 times because, the variable newLink scope is valid only in the method scope. So every time you call this method a new variable is created and then after the method is executed, destroyed. You can't create variable with the same name in the same method or class.

For example this would be invalid:

public void insertFirstLink(String bookName, int millionsSold) {
     Link newLink = new Link(bookName, millionsSold);
     newLink.next = firstLink;
     firstLink = newLink;
     Link newLink = new Link(bookName, millionsSold);
}

Because the variable are declared in the same method.

You can read this to better understand variable scope

EDIT: To loop into this list you can use a simple while:

// boolean used to exit the loop
boolean found = false;
// save firstLink to another object, this way you will not modify the linked list while looping
Link link = theLinkedList.firstLink;

while(!found)
{
      // if line.next != null you have another element in the list, so save it into link and go forward on the loop
      if(link.next != null)
      {
          link = link.next;
      }
      else
      {
          // here you are one the first inserted element
          // set found to true to exit while loop
          found = true;
          //this will print "Don Quixote 500"       
          System.out.print(link.bookName + " " + link.millionsSold);
      }

  }
amicoderozer
  • 2,046
  • 6
  • 28
  • 44
  • If the variable is destroyed after the execution how can we able to get the details of every node in the linked list? I'm confused. – Kusal Dananjaya Sep 01 '17 at 13:23
  • Because in the last line of the method (`firstLink = newLink;`) the value of `newLink` variable is assigned to `firstLink` that is declared as `public Link firstLink;` in class `LinkList` and has class scope. – amicoderozer Sep 01 '17 at 13:36
  • Ok, I added 4 nodes to the list. Now the first Link variable has last added book details ("Harry Potter"). Now I want to get firstLink.next details. But if we deleted the older newLink variables how we are able to access the older ones? – Kusal Dananjaya Sep 01 '17 at 14:04
  • I edited my answer to explain how you can get the older element – amicoderozer Sep 01 '17 at 14:48
  • Thank you. Now I get it. – Kusal Dananjaya Sep 01 '17 at 15:13
0

Objects do not have "names". This line:

Link newLink = new Link(bookName, millionsSold);

simply declares a variable named newLink. Variable name must be unique within the scope of visibility so this is not possible:

public void insertFirstLink(String bookName, int millionsSold) {

    Link newLink = new Link(bookName + " one", millionsSold);
    Link newLink = new Link(bookName + " two", millionsSold);
    ...
}

However this is possible:

public void insertFirstLink(String bookName, int millionsSold) {

    { Link newLink = new Link(bookName + " one", millionsSold); }
    { Link newLink = new Link(bookName + " two", millionsSold); }
    ...
}
lexicore
  • 42,748
  • 17
  • 132
  • 221
0

insertFirstLink() adds a new link to the front of the linked list. You are calling the function four times with different parameters on each call in main. For each one of those calls the function invokes the Link constructor, Link(), with the parameters passed in to the function. Then the newly created Link's next field is set to point at first link in the LinkedList. Then firstlink is set to point at the new link just created.

albertjorlando
  • 202
  • 2
  • 9
0

What do I need to study to understand this part?

You need to study the Scope of Variables in java in detail

Raju Sharma
  • 2,496
  • 3
  • 23
  • 41