简体   繁体   中英

Can I call default constructor from parameterized constructor inside public class in java?

I want to call default constructor from a parameterized constructor inside a public java class.

Can I achieve it?

Use this(); in the first line of the parametrized constructor and it will call your default constructor. Make sure you have default constructor as compiler will not provide one if you declare a parametrized constructor.

For Java: You might mean the constructor without parameters. If so you can use the following code:

public class MyClass {
   // no params constructor 
   public MyClass() {
      ...
   }

   // parametrized constructor
   public MyClass(int p1, String p2) {
       this();
   }
}

Hope this helps

yes you can

public YourClass() {
    public YourClass() { super();}
    public YourClass(int x) { this();}
}

provided you have the same argument constructor. This won't work

public YourClass() {
    public YourClass(int x, int y) { this(); } // compiler error here
    public YourClass(int x) { super(); }
}

Note: super() calls the super constructor (in this case, class Object, because MyClass extends Object implicitly and class Object has a no arg constructor) that matches the same number of arguments.

this() calls the constructor of the current class that matches the same number of arguments.

In Java, the default constructor is the no-argument constructor that's implicitly provided by the compiler. And the compiler won't provide one in case you introduce any constructor with arguments.

In that case you have to explicitly define a no-argument constructor (which is not default by the way, because it's not provided by the compiler), eg public MyClass() { } .

And you can call it from other constructor as this(); , which must be the first statement in the constructor where it's being called.

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");
    }
}

You can't call a default constructor once you've created a constructor that takes arguments. You'll have to create the no argument constructor yourself in order to make a call from the parameterized constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM