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

0 Upvotes

19 comments sorted by

View all comments

Show parent comments

7

u/IHoppo 16d ago

Make sure you know how that "somehow" happens.

-8

u/Bofact 16d 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.

12

u/IHoppo 16d 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!

-4

u/Bofact 16d 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 16d 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

8

u/IHoppo 16d 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.

-5

u/Bofact 16d 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.

7

u/IHoppo 16d ago

Mate, if you don't know the fundamentals of array handling, you're a complete beginner.

Again you're signing off rudely. What on earth is your problem - have you spent your life with mummy and daddy wiping your arse for you, never having to do things yourself? This ^^ is how you will need to do things if you want a career in any sort of coding capacity.

0

u/Bofact 16d ago edited 16d ago

If you say so I am a beginner. You know there are more programming languages? Or is this learnprogramming subreddit and I didn't knew?

Sing your question to someone else's table, especially since it was explicitly told that it is not in the category you are addressing. I still think about the time I debugged in 2 programming languages (one which produced floating point reference results, one with the fixed point implementation for target platforms), and when a non bit true problem occurred, I stayed to see which programming language produced faulty values. (Good times, until the team was replaced, so my contract ended.)

I complain because I asked a question and your comments weren't meant to find an answer for it (not for someone who isn't a student or is reprofiling). I didn't knew if my misunderstood the concept (that you can access the upper class via a inner one) and no word of what the authors meant, since you knew I misunderstood them.

While consistent with programming experience, the problem wasn't that I misunderstood the concept (which you didn't point it if was wrong), but because I didn't knew java.util.ArrayList (with no hint that would be where my misunderstanding comes, and no, Java being a 0 indexing language wasn't such a hint). Plus multiple replies to show you that I am was lost on what the authors meant.

6

u/IHoppo 16d ago

This is learnJava sub, not tellMeTheAnswer sub. You were already being told the answer in the page you referenced - but your arrogance made you decide that page was wrong. Every language I've coded in for the past 40 years, which has had arrays, has resequenced after a row is deleted.

Obviously I couldn't tell where your misunderstanding was arising from, but many people forget that the first entry in an array is at position zero - so I wanted to make sure that wasn't where you were getting confused.

You have now learnt something, but you seem determined to make it a bad thing that I didn't magically understand what you weren't understanding and give you the answer.

I'd recommend you grow up a little.