• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Method Local Inner Classes

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

I have a query regarding the Method Local Inner Classes, from K&B Book for SCJP. In that book, it mentions that Method Local Inner Classes cannot access the local variables of the method , it is declared as local variables will not exist beyond the scope of the method where as inner object can still reside on the heap, even afte the method is removed from the stack. This is just fine. But it also mentions that by making local variables "final" inner classes can access them. Now my question is how can the variable accessible to inner class just by making "final". We use keyword final , to avoid re initialization. Aren't we?. Does by making it final , will it be available to the object beyond the scope of method? I didn't understand this. Can any one please clarify me? Thanks in advance.
 
author
Posts: 23928
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

But it also mentions that by making local variables "final" inner classes can access them.



The wording is a bit off. Basically, if the local variable is "final", the compiler knows that its value will not change. So the compiler will create another variable as part of the instance of the inner class, and it will be initialized to a copy of the final local variable.

The inner class is working with a copy of the variable, not accessing the local variable itself.

Henry
[ September 01, 2007: Message edited by: Henry Wong ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See when you declare something as final,its value will be same throughout the class.So when you declare a local varaible as final,it is available throughout the class,so it is available to the inner class also.when it is not declared as final,its scope is till that method only,so it is not accessible in inner class.
 
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 Mack Sr:
... So when you declare a local varaible as final,it is available throughout the class,so it is available to the inner class also.when it is not declared as final,its scope is till that method only...


Not quite. Declaring a local variable as final does not change its scope.

The important point was made by Henry above: "The inner class is working with a copy of the variable, not accessing the local variable itself."

In other words, the local class says, "I know this value will not change, so I'll just copy its value and take it with me as a 'constant' instead of referencing the original variable." This way, an instance of the local class doesn't have to worry about the original variable being destroyed.
[ September 01, 2007: Message edited by: marc weber ]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry, your explanation is quite convincing. Thanks for that.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic