• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question on Method Local Inner Class

 
Ranch Hand
Posts: 127
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Below excerpt is from Book OCA/OCP 7 guide (Kathy and Bates) page 691:


The local variables of the method live on the stack and exist only
for the lifetime of the method. You already know that the scope of a local variable is
limited to the method the variable is declared in. When the method ends, the stack
frame is blown away and the variable is history. But even after the method
completes, the inner class object created within it might still be alive on the heap if,
for example, a reference to it was passed into some other code and then stored in an
instance variable. Because the local variables aren't guaranteed to be alive as long as
the method-local inner class object is, the inner class object can't use them.
Unless
the local variables are marked final! The following code attempts to access a local
variable from within a method-local inner class:




Compiling the preceding code really upsets the compiler:
MyOuter2.java:8: local variable z is accessed from within inner class;
needs to be declared final
System.out.println("Local var z is " + z);
^
Marking the local variable z as final fixes the problem:
final String z = "local var"; // Now inner object can use it



Here is my question: Even if the variable is final, how will it be able to manage if the inner class Object is passed over to some other code which will live outside the method's scope?

Please clarify.

Thanks, Raghu
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as the MyInner class references variables that are accessible it will work properly. Let's say that doStuff method is called, executed and exited. In the process, an instance of MyInner class is created and is usable by an unrelated class (ClassA). If ClassA then calls seeOuter - it will work properly as long as z is final because the Java runtime can guarantee that the value will never change. In Java 8, the concept of effectively final was introduced. From the Java Tutorials Trail: A variable or parameter whose value is never changed after it is initialized is effectively final. So if you're using Java 8+, you may not necessarily need to include the final keyword.

Also, the inner class can be made accessible by a variety of means - returned from the method, inserted in a shared, accessible collection or class, etc. The excerpt that you provided from the book probably could have been made a little more clear by including how the inner class is made accessible to other objects.
 
Raghavendra Desoju
Ranch Hand
Posts: 127
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Terry,
Thanks for clarification. I guess I was not clear enough while describing my confusion. Please check below code:



Output:-

Outer x: Outer, Method Local z is: Method Local Variable

Here is my question:- My understanding was, whether or not it is a final variable, it's scope ends ends as soon as method gets completed. (i.e. it is a local variable). How come Object was able to access the final/effectively final variable still though method (doStuff) is not available in the stack? (Hope I was clear enough this time...)
 
Author
Posts: 213
10
Scala IntelliJ IDE Netbeans IDE Python Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a variable is marked final, the compiler knows it can never change after initialization. Using this information, the method local variable can safely be copied into a member variable in the inner class. That's what happens, and why the restriction applies.

Of course, the member variable in the inner class is given the same name, so it _looks_ like you're accessing the now-nonexistent-out-of-scope method local variable. But you're not. It's smoke and mirrors. Works very well though.

Note that in Java 8, the restriction is that the variable must be "effectively final" that is, definitely initiialized and never modified.

Also note that object references declared this way do _not_ prevent the object being mutated, it only prevents the variable referring to a different object.

HTH,
Simon
 
Raghavendra Desoju
Ranch Hand
Posts: 127
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Simon, I think it is somewhat clear now
 
reply
    Bookmark Topic Watch Topic
  • New Topic