• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Method-local inner class & final variables

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

in the following eg :


the inner class cannot reference the local variable unless its final..
as the variable will die on the stack after method scope is over..

but i did not understand how it is able to access even the 'final' variable..as even the 'final' variable should die on the stack after method scope..the final 'just' doesnt allow reinitialization of the variable.

maybe i am unclear of the 'final' modifier applied to local variable here..

if anyone can help?
thnx
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the variable is final, its value can be copied into the instance as a constant. This way, the variable doesn't need to be read after the object is created, so the variable can be destroyed when the method exits.
 
Yogesh Mashalkar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that makes things clear!

thanks for the response
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
Because the variable is final, its value can be copied into the instance as a constant. This way, the variable doesn't need to be read after the object is created, so the variable can be destroyed when the method exits.




Please Explain more.I am not getting it.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by yogi maheshnath:
... Please Explain more.I am not getting it.


An instance of a local class can continue to exist after the method exits. But when a method exits, local variables are destroyed. So if the instance tried to read one of those local variables after the method has exited, there would be a problem. However, if the local variable is final, then its value will not change, and so that value can be copied into the instance as a constant. That way, the local variable does not need to be read again after the instance is created.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:

However, if the local variable is final, then its value will not change, and so that value can be copied into the instance as a constant. That way, the local variable does not need to be read again after the instance is created.



Are you talking about the instance of member local class??
Who will copy the value of final variable to instance as we are just reading its value??
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Java runtime takes care of the copying, I believe at the time the method of the inner class is invoked.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sandeep Bhandari:
... Are you talking about the instance of member local class??
Who will copy the value of final variable to instance as we are just reading its value??


Yes, the "instance" I'm referring to is an instance of the local class. The value of the final variable is copied behind the scenes.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variable is defined at classlevel.
Here, final x=y is defines at method level.
Can you clear my doubt please.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Just assume, final x=y means x has become constant for that method and it wont be changed anymore.
Now inner class is using this variable "x" which is nothing but a constant. So, even after method completion, that constant will still be with inner class. You have to treat final x a constant... That s all...
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic