public class Employee { private static int id; private int employeeId; private String name; private String position; private int salary; private String department; public Employee(){ this("A", "B", 1); System.out.println("Empty constructor called"); } public Employee(String name, String position, int salary){ this(name, position, salary, "IT"); System.out.println("Constructor with 3 params called"); } private Employee(String name, String position, int salary, String department){ employeeId = id++; this.name = name; this.position = position; this.salary = salary; this.department = department; System.out.println("Constructor with 4 params called"); } public int getEmployeeId(){ return employeeId; } } public class Test { public static void main(String[] args) { Employee employee = new Employee(); } } 

I do not understand why the information in the console is so strangely displayed:

 Constructor with 4 params called Constructor with 3 params called Empty constructor called 
  • If the answer helped you or solved your problem, accept it by clicking the check mark and the up arrow next to the answer. - Anton Sorokin

2 answers 2

I agree with VBugaenko and allow myself to explain in a bit more detail (for clarification). First of all, the constructor without parameters is called:

 public Employee() 

It has 2 lines of code:

 this("A", "B", 1); 

means calling the second constructor with three parameters, and:

 System.out.println("Empty constructor called"); 

console output of the text "Empty constructor called". So, in this place, this text is not displayed immediately , but first the constructor with three parameters is called β€” it used to be in the first line of the constructor. Further, everything is the same: in a constructor with three parameters, a constructor with four parameters is first called:

 this(name, position, salary, "IT"); 

and the next output to the console "Constructor with 3 params called" is also not immediately displayed until the constructor with four parameters is called. This called constructor (the one with four))) no longer calls another constructor, but instead assigns values ​​to variables:

 employeeId = id++; this.name = name; this.position = position; this.salary = salary; this.department = department; 

and then displays "Constructor with 4 params called" to the console. And after that, the output to the console "Constructor with 3 params called" is worked out in the reverse order, and the last thing is the output to the console from the very first constructor without parameters: "Empty constructor called".

A small clarification - if the presented code is in one file, it will not compile, because Both Test and Employee are declared public, and this is not allowed.

    Everything worked right: a class object was created

     new Employee() 

    and worked the constructor without parameters (which was the message in the "Empty constructor called" console), the constructor with 3 parameters was called in it

     this("A", "B", 1) 

    and it also worked fine (the corresponding message in the console was also displayed), and finally the constructor with 4 parameters was called

     this(name, position, salary, "IT");