OPK
OPK

Reputation: 4180

Overloaded constructors

After reading overloaded constructors from a book, i tired the following code:

public class Employee {

    String name;
    int idNumber;

    public Employee(){
        this("JJ", 0);
        System.out.println(name +" "+ idNumber);
    }

    public Employee(String name, int num){

        this.name = name;
        idNumber = num;
        System.out.println(name +" 2nd "+ idNumber);
    }
}


public class Object1 {

    public static void main(String[] args) {

        Employee emp = new Employee();
    }

}

OUTPUT:

JJ 2nd 0

JJ 0

I am really confused. Why "JJ 2nd 0" printed out first then "JJ 0"?? I created an employee object emp and did not pass any args in the parameter, isn't suppose to call the first constructor first?

Upvotes: 0

Views: 231

Answers (5)

Tayyab Kazmi
Tayyab Kazmi

Reputation: 376

You will have to read about the this keyword for that , Just for starters its like and object of the class your'e using,

                       See in the first constructor this("JJ","0") means that a constructor having two arguments is being invoked so the first line redirects the control into the second constructor that's is why the other line is printed first.

Upvotes: 0

Daniel Kvist
Daniel Kvist

Reputation: 3042

It do call the first constructor first, but the first line in it, is a call to the second constructor. When you create the Employee object from the main method, you call the constructor Employee(), with no arguments (the first one). That constructor first calls the second constructor, which prints JJ 2nd 0, and then, the first constructor prints the line JJ 0. This is how it gets executed:

  1. When the main method is being called, the first constructor, new Employee(), is called.
  2. The constructor calls the second constructor.
  3. The second constructor prints JJ 2nd 0, and then it's done.
  4. The first constructor continues with the next line, which is System.out.println(name +" "+ idNumber);, and prints JJ 0.

Upvotes: 0

Pshemo
Pshemo

Reputation: 124215

new Employee(); is invoking

public Employee(){
    this("JJ", 0);
    System.out.println(name +" "+ idNumber);
}

In this constructor

this("JJ", 0);

is invoking

public Employee(String name, int num)

constructor, which ends with call

System.out.println(name +" 2nd "+ idNumber);.

which is responsible for printing

JJ 2nd 0

When this("JJ", 0); will finish System.out.println(name +" "+ idNumber); will be invoked and you should see another line

JJ 0

Upvotes: 5

M A
M A

Reputation: 72844

The no-argument constructor is calling the other constructor with arguments with the following line:

this("JJ", 0);

Upvotes: 2

Paul Richter
Paul Richter

Reputation: 11072

When you wrote:

... new Employee();

You end up calling the default (no-argument) constructor. The first line of code in that constructor is:

this("JJ", 0);

Which calls the 2-parameter constructor, in which you write

System.out.println(name +" 2nd "+ idNumber);

This is the first of the two output statements your program encounters, and thus is the first thing you see in the console.

After that output statement, the program returns to the default no-argument constructor, and proceeds to execute your other output statement

System.out.println(name +" "+ idNumber);

Which is the "second" output statement you see.

If you step through your code with a debugger, go line by line, you will see exactly how the program is executing, and should demonstrate what I am referring to.

Upvotes: 4

Related Questions