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