class Test {
public Test(Object obj) {
System.out.println("Object");
}
public Test(String s) {
System.out.println("String");
}
public static void main(String[] args) {
new Test(null); //prints String. Why not Object?
}
}
If I add another constructor with argument of type Integer
,or, for that matter any other type, calling new Test(null);
results in compilation error - The constructor Test(Object) is ambiguous
.
Why no error is generated for the above example? On executing it, constructor with argument String
is called. Why constructor with argument type Object
is not called? How this ambiguity is resolved?