Javac weird syntax - expression illegal launch error

I came across a strange error, which I believe is a mistake. Here is a minimal case, please do not comment on the usefulness of the code :)

class Foo { static public <X> int bar() { return 42; } public int baz() { return true ? 42 : ( Foo.<Void>bar() > 42 ? 41 : 43 ) ; } } 

Result:

 err.java:7: illegal start of expression Foo.<Void>bar() > 42 ? 41 : 43 ^ 

I tried the SUN SDK javac 1.6.0_13 and 1.6.0_21.
The error goes away when I either

  • make bar () is not generic (just for curiosity, not really an option)
  • remove the parentheses around the ternary expression on line 7

So it looks like if e is an expression, is it not always true to write (e)?

+8
java javac
source share
4 answers

The published code compiles (and works) just fine for me using Eclipse, but I can confirm that javac unable to compile this. I suspect you found a compiler error in javac .

It is probably a good idea to report this .

+5
source share

I managed to compile it with a little code change. So, I assume that this has something to do with the conditional specification of the operator (which is bit complexity) or an error. But this problem occurs only in the conditional statement.

 class Foo { static public <X> int bar() { return 42; } public int baz() { return true ? 42 : ( ((int)Foo.<Void>bar()) > 42 ? 41 : 43 ); } } 
+1
source share

The error has been around for 3 years, but apparently it will not be fixed in jdk 1.6. However, it is fixed in jdk 1.7 beta 14 (the developer preview is b185, so it is fixed there, I tried).

+1
source share

is the return value, you do not need to specify this by calling the static method:

 class Foo { static public <X> int bar() { return 42; } public int baz() { return true ? 42 : ( Foo.bar() > 42 ? 41 : 43 ) ; } } 
-3
source share

All Articles