• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubts regarding Method local inner class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding method local inner class:

In the below code i cannot access "z" from the inner class- it gives a compilation error, due to scope problem...
as "z" is local variable(blows away once the method gets completed) and for the inner class object that will be even the method gets completed(it will resding in the heap.)



class MyOuter2 {
private String x = "Outer2";
void doStuff() {
String z = "local variable";
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z); //Won't Compile!
} // close inner class method
} // close inner class definition
} // close outer class method doStuff()
} // close outer class


My question is marking the local variable as final resolves the issue?Please anybody exlpain once what happens when a variable is marked as final... will it be going to reside on heap...!!!
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Santu please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it.

When you make a variable final, then the compiler knows that the value of that variable will not change once it is assigned. So the compiler gives the method local inner class a local copy of the variable when you instantiate the method local inner class. Read this discussion if it helps...
 
santu das
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot...it is somehow resolved my doubts..
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ankit /santu ,

the compiler knows that the value of that variable will not change once it is assigned. So the compiler gives the method local inner class a local copy of the variable when you instantiate the method local inner class.



I think,there is a slight correct here that it does not give the local copy of variable but compiling (creating the byte code), the compiler replaces all the existances of final variable with corresponding value. As in the below example -



The code after compilation in byte code would become as mentioned below -



In the inner class the variable would not be available but its value would be replaced at the compile time. I hope this would clear all of your doubts.
 
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic