"Hi, Amigo!"

"Hey, Ellie. Do you have something interesting to tell me?"

"Today we'll talk about how long an object stays in memory, also known as the object's lifetime. After an object is created, it exists (lives) as long as at least one variable is storing its address (there is at least one reference to it). If there are no more references, the object dies. Here are some examples:"

public class MainClass
   public static void main (String[] args)
     Cat cat = new Cat("Tommy");
     cat = null;
     Cat cat1 = new Cat("Sammy");
    Cat cat2 = new Cat("Missy");
    cat2 = cat1;
    cat1 = new Cat("Ginger");
    cat2 = null;

"The Tommy object exists for only one line from its creation. The only variable referencing the object is set to null in the very next line, so the object is destroyed by the Java Virtual Machine (JVM)."

"The Sammy object is stored in the cat1 variable after it is created. Or, more accurately, the variable stores a reference to it. A couple of lines later, this reference is copied to cat2. Then a reference to another object is saved to cat1. Now, only cat2 references Sammy. Finally, the last remaining reference to the object is set to null in the last line of the main method."

"The Missy object exists for only one line after its creation. In the next line, the cat2 variable is set to another value, and the reference to Missy is lost. The object can no longer be accessed, so it is considered garbage by the system (i.e. the object is dead)."

"Once created, the Ginger object exists until the method ends. At the end of the method, the cat2 variable is destroyed, with Ginger being destroyed immediately after that."

"I see."

"But if we create a Cat object inside a method and store a reference to it in an instance variable, then the Cat object will exist as long as it is referenced by another object that is still alive."

"Actually, an object isn't usually immediately destroyed by the system. The Java Virtual Machine performs 'garbage collection' from time to time, destroying objects that have been marked for deletion. More about that process later."

"And, if we no longer want a variable to reference an object, we can set it to null, or assign it a reference to another object."