aptech Luan
aptech Luan

Reputation: 1

Method local inner class - can access non final local variable

Normally, method local inner class can access only final local variable. But in my case, I still can access non final local variable from method local inner class. I've tested in both Eclipse and Netbeans IDE. Can you give me some ideas on this? Thanks in advance.

enter image description here

package test;

public class HelloWorld {
    void display() {
        int x = 24;
        int a = 21;//local variable - *not final*;
        class Local {//method local inner class

        void msg() {
            System.out.println(a);
        }
    }
    Local l = new Local();
    l.msg();
}

public static void main(String args[]) {
    HelloWorld obj = new HelloWorld();
    obj.display();
}
}

Upvotes: 0

Views: 73

Answers (1)

irfan
irfan

Reputation: 896

Starting in Java SE 8, if you declare the local class in a method, it can access the method's parameters.

Upvotes: 2

Related Questions