Ява. Цепочка конструкторов. Почему я должен инициализировать параметр в этом случае()
I have a Java sample presented below. And I have a question: why should I initialize the third parameter in this(name, sal, "Gurgaon"); in the third constructor public Employee1(String name, int sal) ? I mean why i can't put "addr" instead of initializing the third parameter--> this(name, sal, addr); ?
public class Employee1 { public String name; public int salary; public String address; //default constructor of the class public Employee1() { //this will call the constructor with String param this("Chaitanya"); System.out.println("Default"); } public Employee1(String name) { //call the constructor with (String, int) param this(name, 120035); System.out.println(name); } public Employee1(String name, int sal) { //call the constructor with (String, int, String) param this(name, sal, "Gurgaon"); System.out.println(name + " " + sal); } public Employee1(String name, int sal, String addr) { this.name=name; this.salary=sal; this.address=addr; System.out.println(name + " "+ sal + " " + addr); } public static void main(String[] args) { Employee1 obj = new Employee1(); } }
Что я уже пробовал:
Я новичок в Java. И я погуглил, но не смог найти ответа.