1
public class Main {
    public static void main(String[] args) {
        int b=1;
        final int c=2; 
        String s1[] = new String[]{"A","B","C"};

        class InnerMain{                                    
            int a=5;

            public void show(){ 
                System.out.println(s1[0]);
                System.out.println("A:" + a);
                System.out.println("B:" + b);
                System.out.println("C:" + c);
            }
        }

        InnerMain test =new InnerMain();
        test.show();
    }
}

The book I have studied says that a local class can use just final variables and references of method that local class is in. İn this example, I used variable b that isn't final or reference. It ran and I didn't get any error. How? Can someone explain this behavior?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Furkan
  • 117
  • 1
  • 2
  • 6
  • possible duplicate of [Difference between final and effectively final](http://stackoverflow.com/questions/20938095/difference-between-final-and-effectively-final) – DavidPostill Mar 03 '15 at 06:30

1 Answers1

5

Your book is probably outdated. Since Java 8 you can use effectively final local variables.

If you tried to change b anywhere before, after, or in the local class definition, you'd get a compiler error.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724