Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Object Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Constructor "Challenge" / problem" Watch "Object Constructor "Challenge" / problem" New topic
Author

Object Constructor "Challenge" / problem

Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Hello Java Greenhorns and Experts,

I have a question about constructors an example with 2 classes:
=====[CLASS EMPLOYEE]=============================
import java.util.*;
public class Employee {
private static final double BASE_SALARY = 15000.00;
private String name;
private double salary;
private Date birthDate;

// Employee constructors
public Employee(String name, double salary, Date DoB) {
this.name = name;
this.salary = salary;
this.birthDate = DoB;
System.out.println("constructor_1 - name,salary,date" );
}
public Employee(String name, double salary) {
this(name, salary, null);
System.out.println("constructor_2 - name,salary" );
}
public Employee(String name, Date DoB) {
this(name, BASE_SALARY, DoB);
System.out.println("constructor_3 - name,date" );
}
public Employee(String name) {
this(name, BASE_SALARY);
System.out.println("constructor_4 - name" );
}
}

=====[CLASS TEST_EMPLOYEE]=============================
import java.util.*;

public class Test_Employee {
private static final double BASE_SALARY = 15000.00;
private String name;
private double salary;
private Date birthDate;

public static void main (String args[]) {
// declare 4 Employee reference variables
Employee E1;
Employee E2;

// create a new Employee object and let E1 point, refer to the new object
E1 = new Employee("ronald");
System.out.println("object 1 created");

// create a new Employee object and let E2 point, refer to the new object
E2 = new Employee("ronald", 100D );
System.out.println("object 2 created");

} // end main
} // end class
=====[END OF CLASS TEST_EMPLOYEE]=============================

Both classes compile well, without any errors

Question
When i run ...>java Test_Employee I get this output as result:

constructor_1 - name,salary,date
constructor_2 - name,salary
constructor_4 - name
object 1 created
constructor_1 - name,salary,date
constructor_2 - name,salary
object 2 created


To my opinion the output should be
constructor_4 - name
object 1 created
constructor_2 - name,salary
object 2 created

since these are the only constructor methods, that match exactly the parameters of the calling test_object

How is this possible?
1. Does the javac compiler simply "glue all type-matching constructors" together and execute them at runtime?

Can you please explain to me how this exactly it works?
Regards, Ronald
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Ronald:

Please use code tags. People are much more likely to respond to your questions if they don't have to parse unformatted code.

John.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi, This executes well, when you call



from Employee_Test class it will call the constructor of Employee class



inside this constructor you have called the two parameter constructor with the this reference. Therefore the execution control
goes to that



Now in this constructor you have called the another one with three parameter, so the execution control goes to that



inside this constructor everything is initialized and it prints

constructor_1 - name,salary,date

and return control to the two-parameter constructor from where it was called

and prints

constructor_2 - name,salary

again control returned to the one-parameter constructor and prints

constructor_4 - name

and rest of the output will be produced in the same manner.

may be this will help you to know the control flow and constructor chaining..

Vaibhav Mittal
Greenhorn

Joined: Jun 11, 2008
Posts: 23
Imagine it like a stack.Let me tell you the flow of control,may be that will help you understand:
For E1:
1. First calls the one argument constructor which takes string as argument since that is the matching constructor.
2. The constructor

is called next. This in turns calls

3. This is where the values are assigned. Now constructor_1 is printed out along with the values received for the arguments.
4. Since this constructor was called by


constructor_2 gets printed out next.
similarly this constructor was called by

constructor_4 is printed out.

I am sure you can figure it out for E2.
it is easier to think of it as a stack. if the constructor are named constructor_1,2,3,4 then the stack is arranged as follow:
constructor_1
constructor_2
constructor_4
Hope this answers your doubts. if you have any doubts or clarifications please ask.

"Life is good"
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Sudhakar Sharma, Vaibhav Mittal wrote:Hi, This executes well, when you call
inside this constructor you have called the two parameter constructor with the this reference.

THAT is what the error in my mind-frame, That was what i did not noticing Sudhakar && Vaibhav.
I was under the wrong assumption that the Employee class had 4 separate and different single constructors.

Thanks for pointing my "newbie java nose" to the fact that multiple "constructor" of the SAME Object called each other!

I hope i do understand it now the right way (see below)


Thanks both for your crystal clear explanation!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Constructor "Challenge" / problem
 
Similar Threads
Scanner class question
Comparable and Comparator
Overloading Constructors
Sorting by 3 variables dynamically in arrayList
Sorting a Vector by a specific language?