CodeGym /Courses /New Java Syntax /Object lifetime

Object lifetime

New Java Syntax
Level 16 , Lesson 2
Available

"I'd also like to tell you a couple of interesting things about object lifetime. In Java, it's very difficult to accidentally destroy an object. If you have a reference to an object, it is alive.

You can't change references to an object, and you can't increase or decrease them. Additionally, you can't create a reference to an object. You can only assign a reference or set it to null."

"I think I understand, Ellie. So if I erase (or set to null) all references to an object, then I'll never again be able to get a reference to that object or access it, right?"

"That's correct. However, you can also have the situation where the system has too many live objects that are not being used. Programmers often create dozens of objects, store them in various lists for processing, and then never empty these lists.

Objects that programmers don't need are usually simply marked as eligible for garbage collection. Nobody deletes them from lists. As a result, large Java programs often become too big as more and more unused objects remain in memory.

You won't run into this soon, but every time I'll remind you about these unused objects, as well as the right way to dispose of them."

"OK. Thank you, Ellie, for helping me better understand references."

Comments (23)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Henry Level 6, Seattle, United Kingdom
5 January 2023
I love you Codegym
Jaime Padilla Level 41, Chandler, United States Expert
27 April 2021
What do they mean? "So if I erase (or set to null) all references to an object, then I'll never again be able to get a reference to that object or access it, right?" Can't you just go back in the code and set the object back to what you want? Set the object reference back to Cat cat54 = Cat();
Nathan Guidry Level 6, Lake Charles, United States
19 May 2021
talking theoretically for the rest of the code, I suppose
Diego Ortiz Level 8, Lima
15 June 2021
Once you delete all references to an object, the object ceases to exist. And if you create another object with the same characteristics, it would no longer be the same but a new one, in a new memory space. And if you talk about the reference it still exists but it points to another object or null. And when creating a new object using the same reference, the reference still points to another object with the same characteristics.
Steve C. Ands Level 10, Medellín, Colombia
20 December 2021
Sos lo máximo. Gracias por explicar.
Aldo Luna Bueno Level 1, Peru
19 February 2022
They talk about what happends at runtime. If you set to null all references to a Cat object, like this:

Cat cat = new Cat();
lastCatReference = cat;
...
cat = null;
lastCatReference = null;
you just lost the object.
Justice Level 14, Pretoria, South Africa
22 April 2021
Is it possible to access an object before it's destroyed or garbage-collected? I mean, if I stored the memory address of my object in a variable, would I be able to access it? Is it possible?
Jonaskinny Level 25, Redondo Beach, United States
22 February 2022
An object variable would hold a reference to the object, so the object would not be a candidate for GC. If you mean store the object reference string i.e. @234234234, and then let the GC collect the object, then re-animate an object using @234234234 then still no, because the GC removes the reference from the heap when it removes the object, so there is nothing on the heap matching @234234234
Ahmad Level 7, Detroit, United States
3 April 2021
"You won't run into this soon, but every time I'll remind you about these unused objects, as well as the right way to dispose of them." I love the fact that we will continue to learn this very important topic and not overlook it!
Maryem Vickers Level 7, HT..., United Kingdom
27 August 2020
AHHHHHHHHHH I ran outta matter ~<:C
Mihai Bone Level 8, Bucharest, Romania
9 October 2020
What..... you are cheating ? I have 237 matter at this point. Solve your exercises that is the point of this training :D don't be lazy, if not I will come to UK and steal your job. 🤣😂
Nathan Guidry Level 6, Lake Charles, United States
19 May 2021
Literally cheating lol, everyone else worked much harder to get here than you haha
Brandon Leirer Level 7, Keller, United States
10 June 2020
Im not sure about this line: " You can't change references to an object, and you can't increase or decrease them. Additionally, you can't create a reference to an object. You can only assign a reference or set it to null." I feel like I have seen many examples of changing references to and object, and I have certainly seen references increased and decreased. They did this in the example like 2 lessons ago. Cat cat1 = new Cat(someName) ; cat2 = cat1; // this added a second reference to the same object right???
Wojciech Kasiewicz Level 7, Seattle, United States
23 June 2020
No, the reference itself is invisible to you and you assign it's value to the variable you created. In the example you provided the program copied the same reference and stored it in a new variable. A reference is a value corresponding to a specific cell in computer's memory where the object was created, you can have a lot of copies of that value but if you change it, decrease or increase it, it would point to a different slot in memory ergo not the same object anymore. Since a reference is a binary value that points to a specific object, once you loose the reference by overriding your variable with a different value, it becomes impossible for you to access that object since you have no idea where in the memory it actually is. That is also the reason why you can't create a reference on your own, you wouldn't even know where to start. To create an analogy imagine the memory as a junkyard like the one in Inside Out movie. Each time you create an object in Java you tie a rope to it and throw it down there to store it. You don't really know where it is but you don't care much since you have a rope you can use to pull it out if you need the object for something. Each variable would be a person standing on the edge, holding that specific rope. There of course can be a couple people holding the same rope. You can tell them to move around and grab different ropes but if it so happens that there's no one holding a specific rope anymore, it falls down. No variable is able to access it, good luck going into the junkyard yourself to find the object and the corresponding rope, and bringing it back up for a variable to grab. Hope that helps :)
Mecs Level 17, Budapest, Hungary
24 September 2020
It is the best explanation of references I've ever seen!!! Thanks Wojciech!🤟
Stanislav Mayer Level 15, Czech Republic
18 January 2022
I would have never thought I'd have to watch Disney movies to learn Java :-D But thanks Wojciech, that explanation really helped!
P.B.Kalyan Krishna Level 24, Guntur, India
31 March 2022
Good explanation.👍👍
Goran Level 41, Belgrade, Serbia
25 February 2020
And this still warming up :D
ashgolan Level 8, Ashdod, Israel
8 September 2019
it's beginning to be hard :)
Vaishnavi Level 8, Waukesha, United States
2 May 2019
Been there, done that!
Emil Olbinado Level 6, Manila, Philippines
25 April 2019
Emil was here!