• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Why method local inner class can't access non final local variable of method?

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



Can any one please explain, why can't we access method local variable b inside method of Inner class

Thanks in advance.

Have a wonderful day ahead!

Regards,
Gopal
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, does the code compile? Even if you remove the line that causes the compile time error, I don't see how your code compiles.

Henry
[ October 22, 2008: Message edited by: Henry Wong ]
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For method-local inner classes, only "compile time constants" are available. a is final and initialized at declaration, so the compiler knows it at compile time.

b is a normal, stack variable, and is not a "compile time constant". Think at it, outside of the method b will disapear (out of scope). An instance of the internal class could be linked to another object whose lifetime is much much longer,for instance:


After call of m1(list). List will stay alive and be filled with an object of type Outer.Inner, which will have a reference to b who may be garbage collected
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

For method-local inner classes, only "compile time constants" are available. a is final and initialized at declaration, so the compiler knows it at compile time.



This is not true. It doesn't have to be a compile time constant. It just needs it to be a constant.

Basically, during instantiation of the inner class, when both the method and the class is in scope, the inner class will make a copy of the variables which are constants. This means, that the method can go out of scope, since the inner class is only using a copy of the variable.

Henry
 
Ranch Hand
Posts: 220
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int b

in your code will be disappear after your method execution completes, therefore you will be allowed to use only final and instances variables with method local inner classes



[ October 22, 2008: Message edited by: Anut Walidera ]
 
Sheriff
Posts: 9709
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
you can watch my blog jgenius.blogspot.com/ if it would clear your doubt...
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go to this site for the answer:
http://techtracer.com/2008/04/14/mystery-of-accessibility-in-local-inner-classes/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic