In the code below, I have a User defined constructor (no arg constructor) and a parameterized constructor.
What I understand is that if I have at least one constructor, the compiler won't add a default/implicit constructor.
My Question:
In the main Method, I am creating Employee object by calling the parameterized constructor. In the parameterized constructor I am only setting the empId property.
When I try to print the value of name, it prints it as null (i.e. the default value).
What initializes name to NULL (i.e its default value)? It cannot be the implicit/default constructor generated by the compiler, since we have at least one constructor in the class.
public class Employee {
String name;
int empId;
public Employee() {
System.out.println("Calling User Defined Constructor");
}
@Override
public String toString() {
return "name=" + name + " empId=" + empId;
}
public Employee(String name, int empId) {
this.empId = empId;
}
public static void main(String[] args) {
Employee e = new Employee("test",123);
System.out.println(e);
}
}
this.empId = empId;:)