• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method local inner class

 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • 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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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 ?

 
Akanksha Mittal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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

 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If we declare a as final it will compile and run fine.
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
 
Salil Vverma
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
suresh dasari
Ranch Hand
Posts: 121
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic