First of all, use debugger, Java offers nice option for debugging.
If you are using, any editing tools, like, eclipse, or others there are support for debugging. If you want to learn java internal debugging tool that is being used by those application, take a look at jdb.
With debugging tool you can examine each line behavior and observe the result.
I have added some debug information for you to understand; Run the code go through one by one and see the result.
public class Employee1 {
public String name;
public int salary;
public String address;
public Employee1()
{
System.out.println("0 arguments");
this("Chaitanya");
System.out.println("Default");
}
public Employee1(String name)
{
System.out.println("1 arguments");
this(name, 120035);
System.out.println(name);
}
public Employee1(String name, int sal)
{
System.out.println("2 arguments");
this(name, sal, "Gurgaon");
System.out.println(name + " " + sal);
}
public Employee1(String name, int sal, String addr)
{
System.out.println("3 arguments");
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();
}
}
Few other example of creating object based on your current class
obj = new Employee1("Navin");
obj = new Employee1("Navin", 3000);
obj = new Employee1("Navin", 3000, "Australia");
To your question
Quote:
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); ?
This is designed for learning purpose. Once you understand the point of this, you won't have to deal this exactly this way anymore.