0

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();
}
}
1
  • nobody can give me ideas on this? Commented Oct 18, 2017 at 2:58

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.