The rubbish collector can’t accumulate an object till it’s now not accessible to any reside thread.
Given the next code
interface Repairable {}
class Automotive implements Repairable {
class Clutch implements Repairable {}
personal Clutch c;
public Automotive() {
c = new Clutch();
}
public Clutch getClutch() {
return c;
}
}
01: var rl = new ArrayList<Repairable>();
02: var automotive = new Automotive();
03: var clutch = automotive.getClutch();
04: var engine = (Repairable) null;
05: rl.add(automotive);
06: rl.add(clutch);
07: automotive = null;
08: clutch = null;
09: rl.add(engine);
10: rl.set(2, engine);
11: rl.take away(0);
12: rl.take away(1);
After which line will the Automotive occasion created at line 02 grow to be eligible for rubbish assortment? Select one.
A. It can grow to be eligible after line 07.
B. It can grow to be eligible after line 08.
C. It can grow to be eligible after line 10.
D. It can grow to be eligible after line 11.
E. It can grow to be eligible after line 12.
F. It is not going to grow to be eligible after line 12.
A reachable object is any object that may be accessed in any potential persevering with computation from any reside thread.
For this query, it’s ample to contemplate solely the one thread executing this code and the variables which can be declared inside it. There are 4 such variables: rl, automotive, clutch, and engine. From these variables, it is best to think about the transitive reachability. That’s, in case you have added automotive to the checklist, you may get at automotive by means of the reference rl, as a result of you’ll be able to comply with rl to the checklist after which from the checklist you’ll be able to entry automotive. All such transitive reachability should be thought of.
Now, take a look at the state of those variables at key factors within the code.
By the top of line 06, the variable rl refers back to the ArrayList, and that checklist comprises references to each the Automotive and the Clutch objects. At that very same level, the automotive variable additionally refers back to the Automotive object. Clearly the Automotive object is reachable at this level.
By the top of line 10, the automotive and clutch variables have been nulled out—however the checklist nonetheless comprises references to each the Automotive and Clutch objects, so the Automotive object continues to be reachable.
At this level, the checklist’s contents are the automotive, the clutch, and null. Word that line 09 appends engine (which comprises null) to the checklist, putting null at index 2. Then line 10 units the worth at index 2 to the engine worth: null. Line 10, subsequently, has no significant impact on the state of issues.
Line 11 removes the merchandise at index 0, which is the automotive. At first sight, which may appear to be the final reference to the automotive, however you’ll see in a second that it’s not. Persevering with to hint the execution, at this level the checklist now comprises the clutch and null.
Line 12 removes the merchandise at index 1, which is the null. The checklist, subsequently, comprises nothing besides the clutch.
What’s not instantly apparent is that the Clutch object itself, which is an interior class, comprises a reference to its enclosing occasion, which is the Automotive object. Due to that reference, the Automotive object continues to be reachable after line 12, which makes choice F the proper reply and the opposite choices incorrect.
This reference to the enclosing occasion is usable by the programmer. In the event you have been so as to add a toString methodology, corresponding to the next, to the Clutch the reference, Automotive.this would supply entry to the automotive to which this clutch belongs. It’s value noting that in Java, the entry management guidelines are such that the options of a Automotive object are accessible from the Clutch class even when they’re personal, and the options of Clutch are likewise accessible to Automotive—once more, even when they’re personal.
class Clutch implements Repairable {
@Override
public String toString() {
return “Clutch of automotive ” + Automotive.this;
}
}
Conclusion. The right reply is choice F.
Supply: oracle.com