次のコードを検討してください
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);
}
}
jdk-1.6.x を使用していたとき、正常にコンパイルできました。しかし、jdk-1.7にアップグレードすると、エラーが発生します:
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
したがって、このエラーを回避するために、2番目のCtorを次のように変更しました
public Foo (final String str, final Integer value, Object ... bug)
{
this.value = value;
System.out.println ("Should work! 2");
}
整数に自動ボックス化してコンパイルエラーをスキップできるようにします。
いくつかの質問:
1) それは良い習慣ですか? そうでない場合、他の方法はありますか?
2) なぜ Java 開発者は、エラーを許可するのではなく、エラーを出すという決定を下したのでしょうか?