Furkan
Furkan

Reputation: 117

Inner Local Classes in Java

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?

Upvotes: 1

Views: 66

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279910

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.

Upvotes: 5

Related Questions