I want to call default constructor from a parameterized constructor inside a public java class.
Can I achieve it?
You can just call default constructor with new operator (like this: new Test();) or this();. just Test() is forbidden because its not a method of class.
package org.gpowork.test;
public class Test {
private String field;
private Long time = 0L;
public Test(){
this.time = System.currentTimeMillis();
System.out.println("Default constructor. "+this.time);
}
public Test(String field){
this();
Test instance = new Test();
this.field = field;
}
public static void main(String[] args){
System.out.println("start...");
Test t1 = new Test();
System.out.println("-------");
Test t2 = new Test("field1");
}
}