public class Graph {
private Node node;
public void createGraph()
{
}
private class Node<K>{
K data;
List<Node> adjacent;
boolean visited;
Node()
{
adjacent = new ArrayList<Node>();
visited = false;
}
Node(K data)
{
this.data = data;
this.Node();
}
}
}
Why is the compiler complaining that I can’t call this.Node() ?
Try:
The call to the “other” constructor needs to be always first. The call to the other constructors from inside a constructor is always done with
this(...)not withNode(...).JLS section 8.8.7 specify how the constructor body should look like:
where
ExplicitConstructorInvocation(opt)is either an alternate constructor or a constructor from the parent class invoked withsuper(...).and JLS section 12.5 specify the object initialization steps when a constructor is called:
These JLS rules make sure that the parent class constructor is called first and once.