Click here to Skip to main content
15,900,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
    }
}


What I have tried:

I'm a beginner in Java. And I have Googled, but couldn't find an answer.
Posted
Updated 2-Sep-18 23:52pm

1 solution

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.

Java
public class Employee1 {
  public String name;
  public int salary;
  public String address;

  //default constructor of the class
  public Employee1()
  {
      System.out.println("0 arguments");
      this("Chaitanya");
      System.out.println("Default");
  }

  public Employee1(String name)
  {
      //call the constructor with (String, int) param
      System.out.println("1 arguments");
      this(name, 120035);
      System.out.println(name);
  }
  public Employee1(String name, int sal)
  {
    System.out.println("2 arguments");
      //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)
  {
      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
Java
obj = new Employee1("Navin"); // you are giving a name and your constructor is setting up rest of the default values
obj = new Employee1("Navin", 3000); // name and salary, others are default
obj = new Employee1("Navin", 3000, "Australia"); // name, salary and location; no default value


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900