Skip to content

Method-Local Inner Classes

January 16, 2008
tags:

Method local inner class is that class which is declared within the curly braces of a method as follows:

class OuterClass
{

public void method1()
{

class MethodLocalInnerClass
{

public void innerClassMethod()
{
}

}

}

}

same as the method method1() can. The The above class MethodLocalInnerClass declared inside the method method1() is called method local inner class. As the name suggests this class is in the scope of the method only and can access any member of the OuterClass. Important point here is that the MethodLocalInnerClass class cannot use any of the local variables of the method1() (except those which have been marked final). the reason being that the method resides on the stack and as soon as the method life is over all the local variables of the method are also vanished. Now imagine if the method local inner class is using the method methods1()’s local variables and setting them as value of one of its instance variables and then method method1() passes an object of the MethodLocalInnerClass in another method’s invocation as a parameter. In that case the local variable will also be passed as a part of the MethodLocalInnerClass object. Now if the method1’s execution completes, then the local variables are vanished and in that case the object of the MethodLocalInnerClass will be carrying a reference to a variable which does not even exist on the stack anymore.

The following code will explain this scenario more clearly:

public class Test2
{

public static void main(String[] args)
{

System.out.println(foo().toString());

}
static Object foo()
{

Object localVar = new Object();
class Foo
{

public String toString()
{

return “Hi:”+localVar.toString();

}

}
return new Foo();

}

}

If you look properly, you can see that the foo() method of the Test class is returning a reference of the method local inner class Foo declared in the foo() method. So now you can call the toStrin() method of the Foo class from outside the method foo(). So whats the problem here. The problem is that the toString() method of the Foo class is using the local variable localVar which does not exists anymore as the method foo() has already returned and its stack has already been blown up. So this would not even compile. The only solution to make this code compilable is to make the localVar being used in the method local inner class Foo class as final.

One more thing about the method local inner class, that an instance of the method local inner class should be made after full declaration of the method local inner class. The following code will explain what I want to say here:

class OuterClass
{

public void method1()
{

class MethodLocalInnerClass
{

public void innerClassMethod()
{
}

}
MethodLocalInnerClass mlic = new MethodLocalInnerClass(); // remember this instantiation of the method //local inner class came after the class declaration.

}

}

If you try to create an instance before the class declaration, the code won’t compile for obvious reasons here 🙂
That’s it for the method local inner classes.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: