Inner class and local variables

inner-classes, java, local-variables

Solution

The answer is the two are in different scopes. So that variable could change before the inner class accesses it. Making it final prevents that.

Problem

Why do I need to declare a `local variable` as `final` if my `Inner class` defined within the method needs to use it ? Example : ``` class MyOuter2 { private String x = "Outer2"; void doStuff() { final String y = "Hello World"; final class MyInner { String z = y; public void seeOuter() { System.out.println("Outer x is "+x); System.out.println("Local variable is "+y); MyInner mi = new MyInner(); mi.seeOuter(); } } } ``` } Why the String `y` needs to be a final constant ? How does it impact ?

Original source

Related problems