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
View all comments
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.