Reputation: 17
public class LinkedList {
Node head;
Node n;
int size;
public void insert(int data) {
Node node = new Node();
node.data = data;
node.next = null;
if (head==null) {
head= node;
n= head;
size=1;
}
else {
n.next=node;
size++;
n=node;
}
}
what happens to the object node when it reaches the end? will it gets destroyed storing a copy of data inside object n or else it will remain as such?
Upvotes: 0
Views: 63
Reputation: 5754
If there are no references to an object, it is eligible for garbage collection.
If there is at least one reference to an object from some other object that is itself not eligible for garbage collection, then the first object would not be eligible for garbage collection. This protects against creating A that references B, and B references back to A, but neither one of them is has any references from anywhere else, so both could be garbage collected.
Also, just to clarify, being eligible for garbage collection does not mean that an object will actually be garbage collected.
Upvotes: 2