Consider the following code
public class Foo
{
int value;
public Foo (final String str, Object ... bug)
{
System.out.println ("Should work! 1");
}
public Foo (final String str, final int value, Object ... bug)
{
this.value = value;
System.out.println ("Should work! 2");
}
public static void main (String[]args)
{
Foo f = new Foo ("Foo", 3); //Line 14
System.out.println(f.value);
}
}
When I was using jdk-1.6.x I was successfully able to compile it. but upon upgrading to jdk-1.7 it gives me error :
Foo.java:18: error: reference to Foo is ambiguous, both constructor Foo(String,Object...) in Foo and constructor Foo(String,int,Object...) in Foo match
Foo f = new Foo ("Foo", 3); //Line 14
So to avoid this error I changed second Ctor to
public Foo (final String str, final Integer value, Object ... bug)
{
this.value = value;
System.out.println ("Should work! 2");
}
So that it can autobox to Integer and skip the compilation error.
Few question :
1) Is it a good practise ? If not then is there other way ?
2) Why java developer would have taken the decision to give error instead of allowing it ?