Skip to content
Advertisement

Missing custom constructor in Java class [closed]

There is this question on LeetCode that asks you to flatten a multilevel doubly LinkedList. I got stuck in it, so I started googling around. I found a Java solution for it but it doesn’t make sense.

the given Node class doesn’t have any constructor (custom) but in the solution, they are making an object of it, like if it has a custom constructor!

like the following:

class Node {
    public int val;
    public Node prev;
    public Node next;
    public Node child;
};

Node dummy = new Node(0, null, head, null);

how is that possible? it’s like if the Node class has some sort of a hidden constructor!

Advertisement

Answer

You can’t do hidden constructor, specially at compilation.

But, there is few solutions :

  • Use compilation library such as lombok (with @AllArgsConstructor such as Ogod mentionned)
  • Use gradle plugin
  • Create your own constructor like :
public Node(int val, Node prev, Node next, Node child) {
   this.val = val;
   this.prev = prev;
   this.next = next;
   this.child = child;
}

  • IDE like Intellij allow you to automatically generate constructor
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement