Friday, February 25, 2011

Chapter 53: Method Local Inner Classes

In the previous chapter I had promised that we will look in more detail about the different types of inner classes. Well here we are. In this chapter we are going to look at method local inner classes. In other words, classes declared inside the curly braces of a method.

So, lets get started!!!

Method Local Inner Classes

A regular inner class is scoped inside another class’s curly braces, but outside any method code (in other words, at the same level that an instance variable is declared). But you can also define an inner class within a method:
class MyExOuterClass {
private String x = "Outer";

void doSomething() {
class MyExInnerClass {
public void accesOuter() {
System.out.println("Outer x is " + x);
}
}
}

}

The preceding code declares a class, MyExOuterClass, with one method, doSomething(). But inside doSomething(), another class, MyExInnerClass, is declared, and it has a method of its own, accesOuter(). The code above is completely useless, however, because it never instantiates the inner class! Just because you declared the class doesn’t mean you created an instance of it. So to use the inner class you must make an instance of it somewhere within the method but below the inner class definition (or the compiler won’t be able to find the inner class).

Instantiating a Method Local Inner Class

The following code shows how to instantiate and use a method-local inner class:
class MyExOuterClass {
private String x = "Outer";
void doSomething() {
class MyExInnerClass {
public void accesOuter() {
System.out.println("Outer x is " + x);
}
}

MyExInnerClass mi = new MyExInnerClass();
mi.accesOuter();
}
}

What a Method-Local Inner Class Can and Cannot Do

A method-local inner class can be instantiated only within the method where the inner class is defined. In other words, no other code running in any other method—inside or outside the outer class—can ever instantiate the method-local inner class. Like regular inner class objects, the method-local inner class object shares a special relationship with the enclosing outer class object, and can access its private or any other members. However, the inner class object cannot use the local variables of the method the inner class is in.

If you ask me, Why not? Think about it. The local variables of the method live on the stack, and exist only for the lifetime of the method. You already know that the scope of a local variable is limited to the method the variable is declared in. When the method ends, the stack frame is destroyed and the variable is gone. But even after the method completes, the inner class object created within it might still be alive on the heap if, for example, a reference to it was passed into some other code and then stored in an instance variable. Because the local variables aren’t guaranteed to be alive as long as the method-local inner class object, the inner class object can’t use them. Unless the local variables are marked final! The following code attempts to access a local variable from within a method-local inner class.

class MyExOuterClass {
private String x = "Outer";
void doSomething() {
String z = "local variable";
class MyExInnerClass {
public void accesOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z);
// Won't Compile!
}
}
}
}

Compiling the preceding code makes the compiler spew out this error message:
MyExOuterClass.java:8: local variable z is accessed from within inner class;
needs to be declared final
System.out.println("Local variable z is " + z);
^

Marking the local variable z as final fixes the problem:
final String z = "local variable";

And just a reminder about modifiers within a method: the same rules apply to method-local inner classes as to local variable declarations. You can’t, for example, mark a method-local inner class public, private, protected, static, transient, and the like. For the purpose of the exam, the only modifiers you can apply to a method-local inner class are abstract and final, but as always, never both at the same time.

Exam Tip: Remember that a local class declared in a static method has access to only static members of the enclosing class, since there is no associated instance of the enclosing class. If you’re in a static method there is no this, so an inner class in a static method is subject to the same restrictions as the static method. In other words, no access to instance variables

Previous Chapter: Chapter 52 - Inner classes

Next Chapter: Chapter 54 - Anonymous Inner Classes

6 comments:

  1. thanku u r explanation is cool

    ReplyDelete
  2. i have a doubt about the inner class in java can accept all the basic concepts?????????

    ReplyDelete
  3. @ Malleswaran
    I could not understand the question. Can you pls elaborate on your question?

    ReplyDelete
  4. Where are a method's final variables stored? Don't they go out of scope when the function exits? If so, how can a method local class access these variables after they are out of scope? Clarification here will be much appreciated. BTW, keep up the good work; your blog's truly very informative..

    ReplyDelete
  5. @ Shrik

    No. Method final variables are not stored in the heap like method local variables and hence not destroyed as soon as the method finishes execution. That is why it is available for the method local class.

    ReplyDelete
  6. Nice Article, Thanks.

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers