This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Final variables

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is said that local method inner classes cannot have access to the non-final variables of the method in which they are defined.


Then why does this work?
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because w is final int.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened Abhi, are your eyes loosing power ?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, Punit .
Can you tell me why local method inner classes cannot access the non-final variables of the method??
 
Sheriff
Posts: 9704
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
Abhi when you create a method local inner class, it gets a copy of all the final constants of a method and the final parameters passed to the method. it doesn't get a copy of non-final variables. This is why is is mandatory to initialize all the final constants of a method before declaring a method local inner class or an anonymous inner class...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:No, Punit .
Can you tell me why local method inner classes cannot access the non-final variables of the method??



1) As local variables lives on stack, and your innerclass object will live on heap.
2) You can pass your innerclass object's reference outside your method, and it will be still accessible after your method ends.
3) local variables will garbage collected after your method ends.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about final local variables??
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:what about final local variables??



Ankit said na already, that final is not ganna change in its life, so innerclass object takes it copy for ever.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant to ask, what about final local variables which are not compile time constants? Is it the same?? As w is not a compile time constant.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what about final local variables??


If you decompile a program you would see that the final variables are replaced in all the places where it is refered.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhi vijay wrote:I meant to ask, what about final local variables which are not compile time constants? Is it the same?? As w is not a compile time constant.



That is a completely different story, I have seen a post here where sheriffs are talking about Permanent storage where final local runtime variables is stored, you can say final local variable are not stored in stack, so it can be used. There is storage funda behind this concept.
 
Ankit Garg
Sheriff
Posts: 9704
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

Abhi vijay wrote:I meant to ask, what about final local variables which are not compile time constants? Is it the same?? As w is not a compile time constant.



Upto what I understood, you want to ask is whether the inner class get's a copy of the final fields in a method which are compile time constants. Well yes the inner class get's a copy of these fields. And as I said before since the inner class get's a copy of them, so you must instantiate them before you declare the local class (only if the local class uses the field)...

[Edit: added a minute detail ]
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok.Thanks. Ankit and Punit.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not much about it, but Ankit has done lots of R&D over this topic. You can find something in Ankit's blog.

I just made your example:



I decompiled two class file generated by this class.
And got main file here:LocalInnerClassAndFinalVar.class



and inner class file is here:LocalInnerClassAndFinalVar$1ma.class




Ankit could say more about this.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And this one when I put final int w as compile time constant, above was for run-time constant.

Actual code:


Decompiled code:
LocalInnerClassAndFinalVar.class



and decompiled inner class:
LocalInnerClassAndFinalVar$1ma.class

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's very interesting. Maybe I need to start reading Ankit's blog.
I don't really understand the line
val$w = i;
 
Ankit Garg
Sheriff
Posts: 9704
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

Ruben Soto wrote:I don't really understand the line
val$w = i;



It's just a copy of the final field of the method that the local class keeps as a copy for later referencing...
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic