File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method local inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Method local inner class" Watch "Method local inner class" New topic

Method local inner class

indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

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

Joined: Feb 24, 2006
Posts: 6662

Trying to access non final variables will lead to compile time errors. Have you tried writing some code that will do this ?

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Akanksha Mittal

Joined: Jul 29, 2009
Posts: 26
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

Joined: Aug 05, 2009
Posts: 67

If we declare a as final it will compile and run fine.

SCJP6 - 93% SCWCD5 - 97%
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 802

Hi, Indra
See This
Local Class Declaration

SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

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

Salil Verma
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

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

Joined: Aug 05, 2009
Posts: 67
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

Joined: Sep 28, 2004
Posts: 20531

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

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

Joined: Oct 05, 2009
Posts: 121

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.


Sun Certified Java Programmer with 93 percent
I agree. Here's the link:
subject: Method local inner class
jQuery in Action, 3rd edition