Community
Participate
Working Groups
Build 20030910 The following class compiles fine with javac 1.4.1 and our compiler, but it fails with javac 1.4.2: public class TestCaseSwitch { int i = 0; public TestCaseSwitch() { switch (i) { case 0 : class InCaseClass { public void print() { System.out.println("i=" + i); } } InCaseClass icc = new InCaseClass(); icc.print(); break; default : icc = new InCaseClass(); icc.print(); break; } } public static void main(String[] args) { TestCaseSwitch t = new TestCaseSwitch(); } } The failure with javac 1.4.2 is: TestCaseSwitch.java:15: cannot resolve symbol symbol : class InCaseClass location: class TestCaseSwitch icc = new InCaseClass(); ^ 1 error Who is right? Need to check the spec.
jikes 1.18 also accepts this code.
javac 1.4.2 looks inconsistent. If you change the code to: public class TestCaseSwitch { int i = 0; public TestCaseSwitch() { switch (i) { case 0 : class InCaseClass { public void print() { System.out.println("i=" + i); } } Object icc = new InCaseClass(); icc.toString(); break; default : icc = new Object(); icc.toString(); break; } } public static void main(String[] args) { TestCaseSwitch t = new TestCaseSwitch(); } } It compiles fine with javac 1.4.2. If the inner class is not visible (first version of the code), why the local variable icc would be visible (second version)?
See: http://developer.java.sun.com/developer/bugParade/bugs/4717190.html http://developer.java.sun.com/developer/bugParade/bugs/4725650.html http://developer.java.sun.com/developer/bugParade/bugs/4695057.html
I remember javac 1.4.2 build notes mentionning something about solving a defect in this area. I had the impression they had solved a defect in flow analysis where the local type could be used in subsequent catch but captured variable wasn't initialized there. It seems they took a too drastic measure to disallow this. Bug http://developer.java.sun.com/developer/bugParade/bugs/4725650.html clearly shows they solved the flow analysis problem by reducing the scope, and introducing an inconsistency with local variables, in an area where the spec is vague. Jikes and us both agree, Javac is wrong. Closing.