Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call one constructor from another in Java?

Is it possible to call a constructor from another (within the same class, not from a subclass)? If yes how? And what could be the best way to call another constructor (if there are several ways to do it)?

like image 469
ashokgelal Avatar asked Nov 12 '08 20:11

ashokgelal


People also ask

Can we call one constructor from another in Java?

Example 1: Java program to call one constructor from another Here, you have created two constructors inside the Main class. Inside the first constructor, we have used this keyword to call the second constructor. this(5, 2); Here, the second constructor is called from the first constructor by passing arguments 5 and 2.

How would you call a constructor from another?

The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.

How do you call a constructor from another method in Java?

No, you cannot call a constructor from a method. The only place from which you can invoke constructors using “this()” or, “super()” is the first line of another constructor. If you try to invoke constructors explicitly elsewhere, a compile time error will be generated.


2 Answers

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.

like image 146
Jon Skeet Avatar answered Oct 20 '22 17:10

Jon Skeet


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.

like image 25
6 revs, 4 users 74% Avatar answered Oct 20 '22 16:10

6 revs, 4 users 74%