Problem is, your code still can't compile because Inner1 is not a member of the class Outer1 -- it's just a local class in your method. (I think it very odd that the compiler allows this, as I can't think of a useful reason for doing this.)
Remove the amethod() declaration and this should work:
Which means once the method is completed you can no longer access the variables.
So the declaration of "i" inside the method cannot be used by the class, as you might pass a reference of the class elsewhere and it cannot access the local variables.
But declaring the variable as final makes the variable a "compile-time constant" and hence they can be referenced outside.
//int i = 12; no compiler error
public void amethod()
int i = 12;
public void bmethod()
Originally posted by Ulf Dittmer:
I have never seen this notation - has that always been possible?
Since Java 1.1, which introduced inner classes, if I remember correctly.
It's not exactly intuitive or reader-friendly, is it?
What would be a better notation?
Originally posted by Joel McNary:
a local class in your method. (I think it very odd that the compiler allows this, as I can't think of a useful reason for doing this.)
I use that feature for Visitor implementations that need to return (or rather collect) a value:
Works well for me...