Default constructor constructor InterfaceConstructor with zero value

I am trying to do this:

public class Demo{ public Demo() { Demo(null) } public Demo(Interface myI) { ... } } 

I would like the Demo() constructor to call the Demo(Interface) constructor with null , however eclipse complains: "Demo (null) - undefined" on the line where I call Demo(null) . What do i need to change?

+4
source share
2 answers

You are trying to call a method called Demo that you did not define.

eg.

 class A { public A() { this(1); // calls constructor A(int) A(1); // calls method A(int) } public A(int i) {} // constructor A(int) public void A(int i) {} // method A(int) public AA(A a) { return a; } // method A(A) which returns A } 

If you want the constructor to call another, you need to use this() as

 public Demo() { this(null); } 
+4
source

It should not be Demo(null) , but this(null)

+8
source

Source: https://habr.com/ru/post/1443248/


All Articles