Java language spec question: statements
Java has several types of statements such as
declaration statements and expression statements.
The JLS only says "statement" in the syntax for
various control structures including "if".
Yet "declaration statements" cause funky compiler
errors!
I'm wondering if I'm missing something, or is the
Java Language Spec in error, or is javac broken?
<sscce>
import javax.swing.JOptionPane;
public class DeclTest {
public static void main ( String [] args ) {
if ( args.length > 0 )
int result = JOptionPane.showConfirmDialog( null, "foobar" );
}
}
</sscce>
Compiling the above produces this output:
C:\Temp>javac DeclTest.java
DeclTest.java:6: '.class' expected
int result = JOptionPane.showConfirmDialog( null, "foobar" );
^
DeclTest.java:6: not a statement
int result = JOptionPane.showConfirmDialog( null, "foobar" );
^
DeclTest.java:6: illegal start of expression
int result = JOptionPane.showConfirmDialog( null, "foobar" );
^
DeclTest.java:6: ';' expected
int result = JOptionPane.showConfirmDialog( null, "foobar" );
^
4 errors
C:\Temp>javac -version
javac 1.6.0_16
(Removing the "int result = " causes the resulting code to
compile and run without error.)
I know that even if the code were to compile, the scope of
the variable "result" would be that single statement and so
such code is probably useless. But, why isn't it legal?
--
Wayne