Local VS global variables in Java

10,043

Solution 1

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 temporarily newElement and firstElement both pointed to it. It lost newElement when the method exited but firstElement still points to it, as does lastElement 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:

  • variable: piece of paper you can write an address on
  • object: house
  • garbage collector: demolitions crew

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

Solution 2

enter image description herenewElement 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 the addDataPacket() method completes, the newElement reference no longer exists, but the object that it referred to still exists in memory and that object is referred to by firstElement.

Share:
10,043
Hossein Tav
Author by

Hossein Tav

Updated on July 31, 2022

Comments

  • Hossein Tav
    Hossein Tav over 1 year

    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