+ 3

Inner class object can't use the local variables of the method the inner class is in. Why ?

INNER CLASSES

24th Mar 2017, 2:58 PM
Nikhil Kumar
Nikhil Kumar - avatar
4 Answers
+ 13
Are the methods public? Are you using the inner class's object outside the inner class? Need any more hints?
25th Mar 2017, 10:06 AM
Tashi N
Tashi N - avatar
+ 2
Thanks guys for the answers but I got the answer. Actually we can't use local variables because as we know local variable resides on stack and vanishes as soon as method ends but on the other hand inner class object or any object resides on heap so it may be possible that the local variable no longer exists and someone tries to use the local variable via inner class object that is still alive on the heap. So here is the explanation of my question.
25th Mar 2017, 12:58 PM
Nikhil Kumar
Nikhil Kumar - avatar
+ 2
@Tashi N it really does not matter whether methods are public or private since inner classes are a member of the class so they can access private members too and we typically use object of inner class outside inner class because if we don't what will be use of defining the inner classes.
25th Mar 2017, 1:02 PM
Nikhil Kumar
Nikhil Kumar - avatar
+ 2
@luka I don't have code to show. I have read it somewhere in the book and you can easily get one code by yourself. That does not gonna make any difference to the question. The question will remain WHY and just by seeing code one can't predict anything general about INNER CLASSES
25th Mar 2017, 1:04 PM
Nikhil Kumar
Nikhil Kumar - avatar