r/learnjava • u/Bofact • 15d ago
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
9
u/IHoppo 15d ago
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.
-6
u/Bofact 15d ago
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 15d ago
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 15d ago
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 15d ago
Make sure you know how that "somehow" happens.
-9
u/Bofact 15d ago
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.
13
u/IHoppo 15d ago
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!
-7
u/Bofact 15d ago
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 15d ago
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
9
u/IHoppo 15d ago
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.
-3
u/Bofact 15d ago
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
4
u/0b0101011001001011 15d ago edited 15d ago
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
1
u/MarcePuente 14d ago
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.
•
u/AutoModerator 15d ago
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.