+ 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? ???????
3 Réponses
+ 6
Here's an article on constructor chaining in Java.
https://beginnersbook.com/2013/12/java-constructor-chaining-with-example/
+ 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
} }
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