+9

Is it possible to 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)?

11/7/2019 7:17:08 PM

₦ØØⱤ ₳Ⱡł

16 Answers

New Answer

+13

Yes, it is definitely possible to call a constructor within the same class: this(); It's a common technique, e.g. for constructor chaining: https://data-flair.training/blogs/constructor-chaining-in-java/

+8

Since we can create multiple constructors of the same class by changing the method signature. We can call one constructor from another constructor using the this(). If the other constructor accepts an integer value than we can call it by saying this(5) etc. Always remember that this() should be the first line inside the constructor.

+5

an easy workaround would be to see the constructor as a wrapper of a common method. It is useful if you have to initialize the object again somewhere outside the constructor.

+4

+3

Said Unlikely, StackOverflow question is dated 2008.

+3

Here is a code with this() and super(): https://code.sololearn.com/cJI0TrD1mipb/?ref=app

+3

Yes.

+3

There are two types of constructors: Default constructor Parameterized constructor I will explain in one Example class ConstructorDemo { ConstructorDemo()//Default Constructor { System.out.println("D.constructor "); } ConstructorDemo(int k)//Parameterized constructor { this();//-------------(1) System.out.println("P.Constructor ="+k); } public static void main(String[] args) { //this(); error because "must be first statement in constructor new ConstructorDemo();//-------(2) ConstructorDemo g=new ConstructorDemo(3);---(3) } } In the above example I showed 3 types of calling this() call to this must be first statement in constructor This is Name less Object. this automatically calls the default constructor. 3.This calls the Parameterized constructor. Note: this must be the first statement in the constructor

+2

did you asked the same question on StackOverflow? if its yes then you know the answer https://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java

+2

Yes it is possible to call a constructor within same class this() ; It is keyword used to call constructor within same class.

+1

~ swim ~ It was like a deja vu. if not the URL is helpful - gathering as much information as possible from different sources about your targeted programing language is a great way to learn. --------------------------- - practice : Clean code - theory : text = selling ----------------------------- Thewbsite = text

+1

ik heb geen zin; Being a skeptic, I think they rent him from Fiverr

+1

Yes it's possible to call a constructor from another constuctor. I will extend all methods from parent constructor to child constructor and by using this() function we can access the value from parent constructor.

+1

Yes, definitely possible to call one constructor from another, for example a child class constructor can call super class constructor using super keyword

+1

Anu Reddy Read the question once again, he is asking whether you can do that within the same class.

0

Java constructor chaining is a method of calling one constructor with the help of another while considering the present object. It can be done in 2 ways – Within same class: It can be done using this() keyword for constructors in the same class. From base class: By using super() keyword to call a constructor from the base class