How do I call one constructor from another in Java?

ID : 290

viewed : 154

Tags : javaconstructorjava

Top 5 Answer for How do I call one constructor from another in Java?

vote vote

100

Yes, it is possible:

public class Foo {     private int x;      public Foo() {         this(1);     }      public Foo(int x) {         this.x = x;     } } 

To chain to a particular superclass constructor instead of one in the same class, use super instead of this. Note that you can only chain to one constructor, and it has to be the first statement in your constructor body.

See also this related question, which is about C# but where the same principles apply.

vote vote

84

Using this(args). The preferred pattern is to work from the smallest constructor to the largest.

public class Cons {      public Cons() {         // A no arguments constructor that sends default values to the largest         this(madeUpArg1Value,madeUpArg2Value,madeUpArg3Value);     }      public Cons(int arg1, int arg2) {        // An example of a partial constructor that uses the passed in arguments         // and sends a hidden default value to the largest         this(arg1,arg2, madeUpArg3Value);     }      // Largest constructor that does the work     public Cons(int arg1, int arg2, int arg3) {         this.arg1 = arg1;         this.arg2 = arg2;         this.arg3 = arg3;     } } 

You can also use a more recently advocated approach of valueOf or just "of":

public class Cons {     public static Cons newCons(int arg1,...) {         // This function is commonly called valueOf, like Integer.valueOf(..)         // More recently called "of", like EnumSet.of(..)         Cons c = new Cons(...);         c.setArg1(....);         return c;     } }  

To call a super class, use super(someValue). The call to super must be the first call in the constructor or you will get a compiler error.

vote vote

77

[Note: I just want to add one aspect, which I did not see in the other answers: how to overcome limitations of the requirement that this() has to be on the first line).]

In Java another constructor of the same class can be called from a constructor via this(). Note however that this has to be on the first line.

public class MyClass {    public MyClass(double argument1, double argument2) {     this(argument1, argument2, 0.0);   }    public MyClass(double argument1, double argument2, double argument3) {     this.argument1 = argument1;     this.argument2 = argument2;     this.argument3 = argument3;   } } 

That this has to appear on the first line looks like a big limitation, but you can construct the arguments of other constructors via static methods. For example:

public class MyClass {    public MyClass(double argument1, double argument2) {     this(argument1, argument2, getDefaultArg3(argument1, argument2));   }    public MyClass(double argument1, double argument2, double argument3) {     this.argument1 = argument1;     this.argument2 = argument2;     this.argument3 = argument3;   }    private static double getDefaultArg3(double argument1, double argument2) {     double argument3 = 0;      // Calculate argument3 here if you like.      return argument3;    }  } 
vote vote

69

When I need to call another constructor from inside the code (not on the first line), I usually use a helper method like this:

class MyClass {    int field;      MyClass() {       init(0);    }     MyClass(int value) {       if (value<0) {           init(0);       }        else {            init(value);       }    }    void init(int x) {       field = x;    } } 

But most often I try to do it the other way around by calling the more complex constructors from the simpler ones on the first line, to the extent possible. For the above example

class MyClass {    int field;     MyClass(int value) {       if (value<0)          field = 0;       else          field = value;    }    MyClass() {       this(0);    } } 
vote vote

52

Within a constructor, you can use the this keyword to invoke another constructor in the same class. Doing so is called an explicit constructor invocation.

Here's another Rectangle class, with a different implementation from the one in the Objects section.

public class Rectangle {     private int x, y;     private int width, height;      public Rectangle() {         this(1, 1);     }     public Rectangle(int width, int height) {         this( 0,0,width, height);     }     public Rectangle(int x, int y, int width, int height) {         this.x = x;         this.y = y;         this.width = width;         this.height = height;     }  } 

This class contains a set of constructors. Each constructor initializes some or all of the rectangle's member variables.

Top 3 video Explaining How do I call one constructor from another in Java?

Related QUESTION?