optional
optional

Reputation: 3350

Self referential classes and class loader in java

In following class definition in Java

public class Node{

private Node next;
//other fields

// getter setter 

}

In above class definition class contain a link to self type as next.

How this class is loaded in JVM as I see , this is a recursive definition ?

Upvotes: 1

Views: 395

Answers (1)

yeahseol
yeahseol

Reputation: 385

Class loader loads a class only once. The next field of the class simply indicates that the field named next is a Node type.(JVM's static area)
It is the Instance that can have a recursive relation. (JVM's heap area)

Node node1 = new Node();    
node1.setNext(node1);

In this case, there is only one instance created in the Heap area.
The next in the stack area is will point to the address of the node1 instance.

Upvotes: 1

Related Questions