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 Doubt about method local 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 "Doubt about method local class" Watch "Doubt about method local class" New topic

Doubt about method local class

jerry sharma

Joined: Mar 30, 2006
Posts: 23
class Outer
{public void method()
{ int v =34;// if v is final
class MethodLocal
{{System.out.println("MethodLocal class calling local variable v ="+ v);}
new MethodLocal();
public static void main(String [] args)
new Outer().method();

Why it gives output only when variable is final

if v is final out put is

MethodLocal class calling local variable v = 34

if v not final i know the reason but can anybody explain here what "final"
signifies and why it only accesses final variables
Balaji VR
Ranch Hand

Joined: Mar 22, 2006
Posts: 76
The below is stated in page 6 of the Java Inner class Specification document.
Note the final declaration. Local final variables such as array are a new
feature in 1.1. In fact, if a local variable or parameter in one class is referred to
by another (inner) class, it must be declared final. Because of potential
synchronization problems, there is by design no way for two objects to share
access to a changeable local variable

To underdstand what it means, read this link

Happy Learning!

Bala<br />SCJP 1.4 98%<br />SCBCD 1.3 -- 88%
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
google Javaranch and you will find a lots of threads. Here is one.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
I am just posting the words of one of the great senior rancher

You see, the access that inner classes have to local (method) variables and parameters is an illusion. You don't have any access to local variables. What happens behind the scenes is that the Java compiler creates instance variables in your inner class which contain copies of the local variables you "access".

The problem is, if either the method or the inner class were allowed to change its copy of the variable, the different copies would get out of sync and the illusion would be shattered. This is why the compiler forces you to declare the variable final: so that it can freely generate multiple copies and create the illusion that your inner class has access to them.

The inner class implementation is full of such trickery. It had to be, because Sun added them to the Java language without changing the JVM spec. For example, the special access that inner (and nested) classes have to private variables of their enclosing class is... you guessed it... an illusion. Access is granted through special accessor methods generated by the compiler.

If you don't believe me, use javap to disassemble some class files that have inner classes

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
I agree. Here's the link:
subject: Doubt about method local class
It's not a secret anymore!