Can i call one constructor with another constructor that too in same class not in the sub class in java if yes then how? ??????? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Can i call one constructor with another constructor that too in same class not in the sub class in java if yes then how? ???????

24th Apr 2019, 5:27 AM
Vsuniltagore
Vsuniltagore - avatar
3 Answers
+ 6
Here's an article on constructor chaining in Java. https://beginnersbook.com/2013/12/java-constructor-chaining-with-example/
24th Apr 2019, 8:03 AM
silentlearner
silentlearner - avatar
+ 1
class A { String str; A(){ this("1 default string"); // call A(String s) } A(String s){ str = s; } public static void main(String[] args) { A a1 = new A(); System.out.println(a1.str); //1 default string A a2 = new A("2 String"); System.out.println(a2.str); //2 String } }
24th Apr 2019, 6:15 AM
zemiak
0
I don;t understand exactly what you need , but I think you can do it with "this" operator. Check the following example : https://code.sololearn.com/c5FW71TgS7yB/#java
24th Apr 2019, 5:56 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar