CodeGym /Courses /New Java Syntax /Object visibility and null references

Object visibility and null references

New Java Syntax
Level 11 , Lesson 1
Available

"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)
   {
    Tommy
     Cat cat = new Cat("Tommy");
     cat = null;
    
    Sammy
     Cat cat1 = new Cat("Sammy");
    Missy
    Cat cat2 = new Cat("Missy");
    cat2 = cat1;
    
    Ginger
    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."

Comments (17)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
MoJo Level 23, Cairns, Australia Expert
12 January 2024
It's like Schroedinger said: you can put your cat in the garbage but you will never know if it's been collected unless you check the bin
Korlat Level 25, Gasteiz, Basque Country, Spain
8 February 2022
I think I'm really learning a lot with Codegym and I hardly know English. I'm already at level 6!
Shashikant Sharma Level 8, Surat, India
7 August 2021
I Don't Understand It. Can Anyone Explain This Line In Easy Way.

"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."
Jakhongir Ruziev Level 23, Tashkent, Uzbekistan
9 August 2022

class Cat {

    //Instance variable cat
    Cat cat;

    public void createCat() {
        //Creating cat object inside the method
        cat = new Cat();
        //Method ends but object still lives as it is referenced by instance variable
    }

    public static void main(String[] args) {
        
    }
}
Sinisa Level 11, Banja Luka, Bosnia and Herzegovina
24 February 2021
C/C++ don't have automatic garbage, C# has it.
Ilie Babcenco Level 1
8 November 2022
C# is kind of Java, would say its Java's child
Mihai Bone Level 8, Bucharest, Romania
8 October 2020
LVL 6 another 34 LVL and we are ready...😁
Maryem Vickers Level 7, HT..., United Kingdom
27 August 2020
These have lives like us,, but shorter lives; UNlike us.
vishesh yadav Level 7, Mumbai, India
1 May 2020
"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." I didnt understand this part of the lesson.
Brandon Leirer Level 7, Keller, United States
10 June 2020
if the Cat object is inside of a method, and there is a variable inside of a class (instance variable) that is referencing it, then it will continue to be alive. For instance, in the Cat class, there is a variable called "weight" and it is referenced to the cat object that was created in the method, that link will keep the cat object alive.
jaiveer chand Level 7, Mumbai, India
27 June 2019
"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." please help me with the last line
Bryce Lindley Level 9, Olympia, United States
12 September 2019
Cat cat1 = new Cat("Sammy"); cat1 = new Cat("Ginger");
Brandon Leirer Level 7, Keller, United States
10 June 2020
Basically, if an object no longer has anything referencing to it, then nothing can communicate with it anymore, so it becomes useless and gets deleted. Cat cat1 = new Cat("Sammy"); // a new cat object gets made and is referenced by the variable named cat1. cat1 = null // the variable named cat1 is now assigned 'null' (or nothing). This breaks its previous assignment to the cat object. The cat object now has nothing assigned to communicate with it, so it gets deleted.
Arko Sarkar Level 8, Mumbai, India
29 August 2018
"Once created, the «Ginger» object exists until the method ends. At the end of the method, the cat2 variable is used and then Ginger is immediately removed from memory." "That makes sense, Ellie." "But if we create a Cat object inside a method and store a reference to it in a member variable, then the Cat object will exist as long as it is referenced by another object that is still alive." Didn't understand these! 1. At the end of the method, cat2 is set to null and Cat1 was set to Ginger before, so when is Ginger removed from memory? 2. Cat object's reference is stored in member variable, then as per this lesson's explanation when this reference variable is set to null then the object ceases to exist! Then what does as long as referenced by another object mean? Which class's object should this be?
P.B.Kalyan Krishna Level 22, Guntur, India
21 November 2018
Ginger is removed from memory after the completion of the main() method. He probably means that an object can have multiple references. The object is alive as long as even one object reference variable of the same class exists which refers to the object. If it is a member variable the object exists till the end of program completion or up to that time it is set to null or it is made to refer to another object. Only reference variables of the objects of the same class.
Deep Maheshwari Level 10, Nanded, India
7 June 2019
Can you provide an exemple code demonstrating the statement :- "But if we create a Cat object inside a method and store a reference to it in a member variable, then the Cat object will exist as long as it is referenced by another object that is still alive."
Vahan Level 41, Tbilisi, Georgia
12 June 2019
I think smth like this: Cat cat = new Cat(); cat.name; where name is a member variable.