Tag Archives: Final

Method local inner class can access only final local variables

A method local inner class can be instantiated only from within the method where it is declared after its defination.Since we know that all the local variables resides on stack which gets blown once the method completes,but the object of inner class resides on heap and we can hold a refernce to it by passing it to some other method as a argument thus even after the method call completes we can have access to instance of method local inner class but no access to local variables

What happens when the local variables are declared final?
when you declare the local variables as final they still reside on stack but this time when the inner class needs the access to final local variables the value of the variable is copied from stack to heap when the inner class is instantiated by declaring local variables¬† final we are preventing the object from modifying it’s value as there is also a copy present which will be uneffected.