Community
Participate
Working Groups
Hi. I am not sure, whether that is a compiler bug or if just the Java language spec is a bit flawed. I tried to do the following: public class Test1 { static class A{} static class B extends A{} static class C extends A{} public static void main(String[] args) { A a = false ? new B() : new C(); } } Javac compiles fine here. Eclipse however tells that B and C do not have equal types, which is correct, but actually unnecessary, because they both have a common supertype A. The following also compiles fine in Eclipse: public class Test1 { static class A{} static class B extends A{} static class C extends A{} public static void main(String[] args) { A a; if(false) a = new B(); else a = new C(); } }
Javac 1.4.2 issues an error as well: X.java:9: incompatible types for ?: neither is a subtype of the other second operand: X.B third operand : X.C A a = false ? new B() : new C(); ^ 1 error I suspect you did compare to javac 1.5.0, which now accepts it, like we do when toggle into 1.5 compliant mode. Please reopen if wrong.