psisodia
psisodia

Reputation: 1177

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?

Upvotes: 15

Views: 48966

Answers (7)

Yes, It is possible but few points need to be followed.

  1. Once you create a parameterized constructor java compiler will no longer provide a default constructor for object creation.
  2. So calling it a default constructor would be an inappropriate name. It is called as 'No-args constructor'.
  3. No-args constructor has to be explicitly written by you along with parameterized constructor in order to have a nested calls.
  4. Call to one constructor from another needs always be made as the first instruction of the constructor.
public class Demo {
    Demo(){
        //Instructions...
    }

    Demo(Integer dummyArg){
        //First instruction should be constructor if there is one
        Demo(); 
        //Other instructions...
    }

    Demo(String dummyArg){
        //First instruction should be constructor if there is one
        this(); 
        //Other instructions...
    }

    Demo(Object dummyArg){
        //First instruction should be constructor if there is one
        this("Hello"); //call to constructor with String as arg 
        //Other instructions...
    }
}

Upvotes: 0

Petro Gordiyevich
Petro Gordiyevich

Reputation: 307

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

Upvotes: 3

Abubakkar
Abubakkar

Reputation: 15644

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.

Upvotes: 39

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

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), e.g. 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.

Upvotes: 2

pratZ
pratZ

Reputation: 3346

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.

Upvotes: 1

George
George

Reputation: 497

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.

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42441

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

Upvotes: 8

Related Questions