• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

final local variable question

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know the logic why the variable "ai" from the code below is not visible to the inner class "Inner". Thanks!

 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method local classes can only access local variables that are declared final.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An instance of a local class could still be reachable after the method exits and its local variables are destroyed. If local variables are final, then their values can be copied into the instance.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really can not understand this..

If local variables are final, then their values can be copied into the instance.


Means if the varibale is non final wont be its value be copied into instances?Will the final variable wont lost after the method finises?May be this question sounds silly ... please help!
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might not be obvious how an instance of a local class could still be reachable after the method exits. The key is in upcasting the reference to a type that exists outside the method's scope, as illustrated here...
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The invoked methods(and their variables) goes on to the call stack.Classes once loaded, resides on the heap. Method local classes are no exception to this. Method variables survive till method itself is alive after which they are flushed from the stack. But local classes may still have their reference with other variables hence they cannot be destroyed. And thats the reason why local classes can only access final method variables so that the method lives or dies final var's value will always be the same. If at all local classes were holding non-final variables, and if anybody tries to change its value, jvm will throw exceptions as method may not be alive.
 
Hold that thought. Tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!