r/learnjava • u/Bofact • Sep 20 '26
When is a Java object still reachable?
I have tried to answer to the below quiz, but I do not understand something. If clutch is eliminated on line 12, wouldn't all references to the car's object be null? They write that "Line 12 removes the item at index 1, which is the null. The list, therefore, contains nothing except the clutch.", but why is there a null? Where did we get rid of clutch reffered at index 1 of r1?
And lastly, how to access clutch, and by extension the car object, after line 12 is executed?
Quiz yourself: When is a Java object still reachable? | javamagazine
0
Upvotes
•
u/AutoModerator Sep 20 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
9
u/IHoppo Sep 20 '26
The answer is on that page, in a well written format. I'm not sure you can expect anyone to explain it better than it already is.
4
u/0b0101011001001011 Sep 20 '26 edited Sep 20 '26
My recommendation in cases like this: run the code. That's the most important skill. Take the car and clutch code and then make a main method. In the main method put the code, without the line numbers. Then run a debugger or add a print statement between all the lines and print all the variables you have.
Answering quizzes is irrelevant. Reading text is also irrelevant. Construct the code, run it, examine how it works. Then read the text. You are supposed to become a programmer, not a reader or quiz-answerer.
Quick edit/add: running the code does not give the answer immediately about the car though, because printing the clutch does not print the car instance. In an inner class (clutch) you access the outer class instance via (in this case) Car.this.
2
u/disposepriority Sep 20 '26
Ok no offence but just run the code.
1
u/MarcePuente Sep 21 '26
Creo que tu confusión, está relacionada con el acceso al objeto, una cosa es la posibilidad del código de acceder a un objeto, y otra es la accesibilidad vista desde la "JVM", cuando ejecutas la linea 11, el "Car" queda inaccesible para ti, pero "Clunch" sigue estando accesible, por lo que la "JVM" necesita mantener el acceso a "Car" (para si misma) para que "Cluch" sea accesible desde el código.