I thought I understood the difference between local and global variables in java until I saw an example today. In this code one tries to add elements to a link list in a method:
public void addDataPacket(DataPacket data){
PacketQueueElement newElement = new PacketQueueElement(data);
if(firstElement != null){
lastElement.setNextElement(newElement);
lastElement = newElement;
}
else{
firstElement = newElement;
lastElement = newElement;
}
}
What I don't understand is why the newElement does not disappear after the method is closed? Because this is a local variable and is not defined in the class anywhere. Here is the complete code of this class:
public class PacketQueue {
/** Das erste Element in der Warteschlange */
private PacketQueueElement firstElement;
/** Das letzte Element in der Warteschlange. */
private PacketQueueElement lastElement;
/**
* Instanziert eine neue Warteschlange.
*/
public PacketQueue(){
this.firstElement = null;
this.lastElement = null;
}
/**
* Fuegt ein neues Paket ans Ende der Warteschlange an.
*
* @param data Das neue Paket
*/
public void addDataPacket(DataPacket data){
PacketQueueElement newElement = new PacketQueueElement(data);
if(firstElement != null){
lastElement.setNextElement(newElement);
lastElement = newElement;
}
else{
firstElement = newElement;
lastElement = newElement;
}
}
/**
* Entfernt das erste Element der Warteschlange und gibt es zurueck.
*
* @return Das erste Element in der Warteschlange
*/
public PacketQueueElement getAndRemoveFirstElement(){
PacketQueueElement element = this.firstElement;
this.firstElement = element.getNextElement();
return element;
}
/**
* Gibt das erste Paket aus dem ersten Element zurueck.
*
* @return Das erste Paket
*/
public DataPacket getFirstDataPacket(){
return this.firstElement.getData();
}
/**
* Entfernt das erste Paket der Warteschlange und gibt es zurueck.
*
* @return Das erste Paket in der Warteschlange
*/
public DataPacket getAndRemoveFirstDataPacket(){
return this.getAndRemoveFirstElement().getData();
}
/**
* Gibt das erste Element der Warteschlange zurueck
*
* @return Das erste Element
*/
public PacketQueueElement getFirstElement(){
return this.firstElement;
}
/**
* Ueberprueft, ob die Wartschlange leer ist.
*
* @return true, wenn sie leer ist
*/
public boolean isEmpty(){
if(firstElement == null){
return true;
}
else{
return false;
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString(){
PacketQueueElement element = this.firstElement;
String s = "";
while(element != null){
s += element + "\n";
element = element.getNextElement();
}
return s;
}
}
Thank you in advance
This is mixing up variables and objects,
newElement
is indeed a local variable and it is lost after the method ends, but the reference points to an object. An object is eligable for garbage collection if no references (variables) point to it. In this case temporarilynewElement
andfirstElement
both pointed to it. It lostnewElement
when the method exited butfirstElement
still points to it, as doeslastElement
so it isn't eligable for garbage collection.Or in other words: A variable refers to an object, it is not the object itself.
An analogy:
I build a house and write it's address on a scrap of paper so you can get there, I pass that scrap of paper to you, you write the address from the scrap of paper into your address book, you throw away the scrap of paper.
The demolitions crew checks whether anyone is still using a house by seeing if anyone still holds its address. Even though you threw away the scrap of paper you still have the address in your address book so the house is still being used and is not demolished
newElement
is just a reference to an object created in memory.firstElement
then holds a reference to the same object.The reference
newElement
is indeed a local variable, but the object referred to by the reference is then also referred to by another reference, i.e.firstElement
. So after theaddDataPacket()
method completes, thenewElement
reference no longer exists, but the object that it referred to still exists in memory and that object is referred to byfirstElement
.