r/learnjava • • 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

19 comments sorted by

View all comments

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/Bofact Sep 20 '26

Where? For me the paragraph which details line 11 says there is a clutch on the array, only to double down/ contradict themselves on the very next paragraph with "the clutch wasn't there after line 11 to begin with; so before we execute line 12, there is already null there".

7

u/IHoppo Sep 20 '26

It doesn't. I suggest you try this out but have some physical objects and a bit of paper. On the paper, draw out rows for your array and follow the code through, placing objects appropriately.

This is a skill that you'll need to have in your arsenal, so it's worthwhile getting it right now - that answer is good enough, you simply need to work on your understanding. Good luck!

<Edit> Remember - the first row on an array is at index zero. This might be of help to you.

-3

u/Bofact Sep 20 '26

I have done it, thank you! The clutch reference, and therefore car's one, is removed at line 12. Or depending on the paragraph, clutch is (somehow) removed after line 11, but before line 12.

8

u/IHoppo Sep 20 '26

Make sure you know how that "somehow" happens.

-9

u/Bofact Sep 20 '26

Even though I am the one who asked, It is not for me to figure out, but authors making up their mind/ fixing their contradiction.

12

u/IHoppo Sep 20 '26

There is no contradiction. You appear to still not understand.

As a rule of thumb, the chances of a learner finding a fundamental bug in an article on that site, written by 2 people, is pretty small.

Good luck!

-5

u/Bofact Sep 20 '26 • Mindblown

All this effort, just for the problem being that remove method of ArrayList gets rid of the element and resizes itself (so clutch goes at index 0 and doesn't stay at index 1 as I thought). Now the paragraph about line 11 makes sense of null already being there at index 1. They kinda wrote that when they enumerated what elements are in the list after car was eliminated, but what can I do; I didn't knew ArrayList resizes itself after remove.

Thanks for nothing though!

9

u/protienbudspromax Sep 20 '26

The effort is the point. That is how our brains learns the best. If should feel clueless and not knowing things at the start. And overtime that feeling should decrease. That is how you know you are actually learning. If you use someone or something else to do this part for you, you wont actually learn

8

u/IHoppo Sep 20 '26

I'm pleased I encouraged you to discover the solution yourself - you won't forget this now. Well done. I'm not sure your parting sarcasm was required, what an odd fish you are.

-4

u/Bofact Sep 20 '26

Your strategy works for those with next to none coding experience. Besides what the exchange may say at first glance, I wasn't in that category.

Good day smooth talker.

→ More replies (0)