This is part of the Java visibility and visibility rules. In your current example, this is called Closure . Basically, everything that is defined in a block is visible inside this block. For example, this is valid Java code (anywhere in the method):
{ final int i = 5; // do somthing with i } // cant refernce i here { final int i = 6; // do somthing with i } // cant refernce i here
Since you are defining a new class inside a block (I would not work if you just initiated it), it sees everything that was declared in one block. The only limit to Java that it refers to is that you cannot change the value after the first assignment in order to avoid problems when the link "escapes" its block (multithreading, lives longer than the declaration block, etc.). If you declare the parameters of the final method, you can also use them because they are defined in one block.
So, if you changed the code to this
{ final int[] array = {1,6,3,5,7,8,4,0,3}; } Object inter = new Object() { public void test() { System.out.println(array); } };
This will not work (try).
source share