• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method local inner class

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

It is said that a method local inner class cannot use the local variables of that method however if the variable is marked final then it can.

Please explain the difference between the two i.e accessing the final and non final local variables by a method local inner class.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trying to access non final variables will lead to compile time errors. Have you tried writing some code that will do this ?

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

I think the reason is that final variables do not exit from memory even after the main method exits, and if the inner classes use the local variables, they would not have access to the destroyed variables. The anonymous class which you are creating can still exist after that.

There is a nice piece of information about inner classes here, which might help:
Inner Classes

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


If we declare a as final it will compile and run fine.
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Indra
See This
Local Class Declaration
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Indra,

I believe, your question that how does final make a variable so special that method local class start accepting that parameter.

The answer to that question is hidden in the way java compiler treats the final variable. These are constants so while making a class file the compiler replaces the existence of the final variable with the corresponding value.

Take the example below code -


When java compiles the file it replaces all further exisstances of x with its value. The code of corresponding would be



This is why it gives performance gain than using normal variable in real time environments. Let me know if I could answer your query
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Salil,

Got that however I am still confused as what happens when the variable is not marked as final. Why the local variables not marked as final could not be accessed by method local inner class?

I will really appreciate if you could clarify it.

I have tried the code and I have encountered the error also but I want to know the reason for the difference.

 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because any local variable not marked as final would not live after the method completes. But the method-local inner class' instance could still be in use even after the method completes running.
 
author
Posts: 23951
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
As mentioned, the scope of a local variable is different from the scope of an instance of the inner class -- as it is possible of the instance of the inner class to still exist, even after the local variable has gone out of scope.

This is true regardless of whether local variable is final or not.... ie. there is nothing about a final local variable that makes it live longer in scope than a non-final local variable.


However, if the variable is final, the Java compiler knows that the variable doesn't change, and can track whether it has been initialized... and can make a copy of the local variable, internal to the inner class, at the time when the inner class is instantiated, when they are both still in scope.

Henry
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright.

Main point to know is that the compiler can make local copy of the final variable which will be internal to the inner class whenever it is instantiated.
And it is not the same when the variable is not declared final.
 
Ranch Hand
Posts: 121
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation regarding final and non final method local variables usage in method local inner classes.
my doubt was very much cleared after reading Harsh Pensi's explanation as it is basic knowledge that a local variable scope will be ended as control exits the method which makes the java compiler to throw an error when non final local variable is used.

Cheers,
Suresh.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic