can the method local inner class object access method local variables

/* First of all, your program compiles and works fine because you are using Java 8. 
If using Java 7 or lower, it won't even compile. The reason is exactly as you cited. 
But I will try to explain it a bit more. Consider the following code: */

public void m1() {
    int k = 30;
    class inner {
        public void m2() {
            System.out.println(k); 
        }
    }
    inner o = new inner();
    k = 42;     // <= Note the reassignment here.
    o.m2();
}

/*
What should the method call o.m2() print? "30" or "42"? Both outputs could 
reasonably be argumented. At the time the method was declared and defined, 
the variable k had the value 30. At the time the method was called, the 
variable k had the value 42.

To prevent such ambiguities, the compiler does not allow assignment to a 
variable that is used in such inner classes (local and anonymous). So it 
must be final.

In Java 8 this was relaxed a bit. Java 8 introduced the concept of 
effectively final. A variable that is declared and initialized and not 
being assigned again is considered effectively final. And the compiler 
allows that code without declaring the variable final.

As a matter of fact, you also get a compiler error in Java 8 when trying 
to compile the above code.  
*/

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source