In the code below for linked list
Why doesn't Link newLink = new Link()
give an error the second time thelist.insertfirst()
is called as the newlink
is already defined?
MY UNDERSTANDING - Scope of the variable newlink
is the method insertfirst()
.
Link newLink = new Link(id, dd);
When a link newlink
is created the variable newlink
holds the memory address of the link object created.
newLink.next = first;
first
is a link variable which contains the memory address of a link object.
Then, the newlink.next()
is pointed towards the object contained at the memory address of the variable first
.
first = newLink;
Then, the variable first
is pointed towards the newlink
. This means that first now contains the memory address of the object newlink
(newlink itself contains the address to actual object).
When the method is finished, The variable 'newlink' is lost but we don't care as we have already copied
the memory address (in the next
field) of the link objects.
Is this analogy correct?
class Link
{
public int iData; // data item
public double dData; // data item
public Link next; // next link in list
public Link(int id, double dd) // constructor
{
iData = id; // initialize data
dData = dd; // ('next' is automatically
} // set to null)
////////////////////////////////////////////////////////////////
class LinkList
{
private Link first; // ref to first link on list
public LinkList() // constructor
{
first = null; // no links on list yet
}
// insert at start of list
public void insertFirst(int id, double dd)
{ // make new link
Link newLink = new Link(id, dd);//######################################## DOESN'T THROW ERROR
newLink.next = first; // newLink --> old first
first = newLink; // first --> newLink
}
} // end class LinkList
////////////////////////////////////////////////////////////////
class LinkListApp
{
public static void main(String[] args)
{
LinkList theList = new LinkList(); // make new list
theList.insertFirst(22, 2.99); // insert four items
theList.insertFirst(44, 4.99);
theList.insertFirst(66, 6.99);
theList.insertFirst(88, 8.99);
} // end main()
} // end class LinkListApp
////////////////////////////////////////////////////////////////
You are correct that there is no error here. It's very, very common to have a local variable in a method and then call that method several times. And yes, you may say that it is not the same variable the second time you call the method since the variable is created every time the method is executed.
The reason why you cannot declare two variables with the same name in the same scope is that then you wouldn't know which is which. This problem is not in your code. Each time your method is called, a variable named newLink
is created and assigned a value. So when in the following two lines you use the name of this variable, it is of course the newly created one. It's not the one that was created the last time the method was called, nor the time before the last time. No confusion is possible. This is the reason for allowing it.
Scope is about where in the program, the text if you will, you can use a certain name. In this case between its declaration and the nearest enclosing right curly brace three lines further down. Scope is not about how many times you are allowed to invoke that piece of code and create a new variable with the same name each time. In other words, within one scope you may only type the declaration of a variable with a certain name once when you write the program, but you may use that declaration for actually creating variables as many times as you like when the program is running.
And yes, your description, your analogy is correct.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.