Sunday, July 6, 2008

Inner Classes

In Object Oriented programming, for reuse and flexibility/extensibility you need to keep your classes specialized.

In other words, a class should have code only for the things an object of that particular type needs to do; any other behavior should be part of another class better suited for that job.

Sometimes, though, you find yourself designing a class where you discover you need behavior that belongs in a separate, specialized class, but also needs to be intimately tied to the class you're designing.

One of the key benefits of an inner class is the "special relationship" an inner class instance shares with an instance of the outer class.

That "special relationship" gives code in the inner class access to members of the enclosing (outer) class, as if the inner class were part of the outer class.

In fact, that's exactly what it means: the inner class is a part of the outer class. Let's look at each of them.

Regular Inner Classes

A normal "regular" inner class is declared inside the curly braces of another class, but outside any method or other code block.

class MyOuter {
class MyInner { }
}

An inner class is a full-fledged member of the enclosing (outer) class, so it can be marked with an access modifier as well as the abstract or final modifiers. (Never both abstract and final together— remember that abstract must be subclassed, whereas final cannot be subclassed).

An inner class instance shares a special relationship with an instance of the enclosing class. This relationship gives the inner class access to all of the outer class's members, including those marked private.

class MyOuter {
private int x = 7;
// inner class definition
class MyInner
{ public void seeOuter()
{ // Yes you can access the private variables of enclosing class
System.out.println("Outer x is " + x);
}
} // close inner class definition
} // close outer class

To instantiate an inner class, you must have a reference to an instance of the outer class to tie to the inner class.

An inner class instance can never stand alone without a direct relationship to an instance of the outer class.

From code within the enclosing class, you can instantiate the inner class using only the name of the inner class, as follows:

MyInner mi = new MyInner();

From code outside the enclosing class's instance methods, you can instantiate the inner class only by using both the inner and outer class names, and a reference to the outer class as follows:

MyOuter mo = new Myouter();
MyOuter.MyInner inner = mo.new MyInner();

or

MyOuter.MyInner inner = new MyOuter().new MyInner();

From code within the inner class, the keyword this holds a reference to the inner class instance.

To reference the outer this (in other words, the instance of the outer class that this inner instance is tied to) precede the keyword this with the outer class name as follows:

MyOuter.this;

Method Local Inner Classes

A method-local inner class is defined within a method of the enclosing class.

class MyOuter2 {
private String x = "Outer2";
void doStuff() {
class MyInner {
public void seeOuter()
{ System.out.println("Outer x is " + x);
} // close inner class method
} // close inner class definition }
// close outer class method doStuff()
} // close outer class

For the inner class(Method - Local Inner Class) to be used, you must instantiate it, and that instantiation must happen within the same method, but after the class definition code.

class MyOuter2 {
private String x = "Outer2";
void doStuff() {
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
} // close inner class method
} // close inner class definition
MyInner mi = new MyInner(); // This line must come
// after the class
mi.seeOuter();
} // close outer class method doStuff()
} // close outer class

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 unless those variables are marked final.This is bcoz 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 blown away and the variable is history. 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 only modifiers you can apply to a method-local inner class are abstract and final. (Never both at the same time, though.)

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.

Anonymous Inner Classes

Anonymous inner classes have no name, and their type must be either a subclass of the named type or an implementer of the named interface.

Type - 1 (Subclass of the named type)

class Popcorn {
public void pop() {
System.out.println("popcorn");
}
}class Food {
Popcorn p = new Popcorn()
{
public void pop() {
System.out.println("anonymous popcorn");
}
};
}
Polymorphism is in play when anonymous inner classes are involved. You can only call methods on an anonymous inner class reference that are defined in the reference variable type!

This is no different from any other polymorphic references, for example,

class Horse extends Animal{
void buck() { }
}
class Animal {
void eat() { }
}
class Test {
public static void main (String[] args)
{
Animal h = new Horse();
h.eat(); // Legal, class Animal has an eat() method
h.buck(); // Not legal! Class Animal doesn't have buck()
}
}

Type - 2 (Implementer of the specified interface type)

interface Cookable {
public void cook();
}
class Food {
Cookable c = new Cookable()
{ public void cook()
{ System.out.println("anonymous cookable implementer");
}
};
}
Anonymous interface implementers can implement only one interface.

Type - 3 (Argument defined anonymous inner classes)

class MyWonderfulClass {
void go() {
Bar b = new Bar();
b.doStuff(new Foo() {
public void foof() { System.out.println("foofy");
} // end foof method
}); // end inner class def, arg, and b.doStuff stmt.
} // end go()
} // end class
interface Foo {
void foof();
}
class Bar {
void doStuff(Foo f) {}
}
An argument-local inner class is declared, defined, and automatically instantiated as part of a method invocation.
The key to remember is that the class is being defined within a method argument, so the syntax will end the class definition with a curly brace, followed by a closing parenthesis to end the method call, followed by a semicolon to end the statement: });

An anonymous inner class is always created as part of a statement; don't forget to close the statement after the class definition with a curly brace.

This is a rare case in Java, a curly brace followed by a semicolon.Because of polymorphism, the only methods you can call on an anonymous inner class reference are those defined in the reference variable class (or interface), even though the anonymous class is really a subclass or implementer of the reference variable type.

An anonymous inner class can extend one subclass(type-1) or implement one interface(type-2), Unlike non-anonymous classes (inner or otherwise), an anonymous inner class cannot do both. In other words, it cannot both extend a class and implement an interface, nor can it implement more than one interface.

Static Nested Classes

Static nested classes are inner classes marked with the static modifier.

class BigOuter {
static class Nested { }
}

A static nested class is not an inner class, it's a top-level nested class.The class itself isn't really "static"; there's no such thing as a static class. The static modifier in this case says that the nested class is a static member of the outer class.

That means it can be accessed, as with other static members, without having an instance of the outer class. Because the nested class is static, it does not share any special relationship with an instance of the outer class.

In fact, you don't need an instance of the outer class to instantiate a static nested class.Instantiating a static nested class requires using both the outer and nested class names as follows:

BigOuter.Nested n = new BigOuter.Nested();

Example :

class BigOuter {
static class Nest {void go()
( System.out.println("hi");
}
}
}
class Broom {
static class B2 {void goB2() {
System.out.println("hi 2");
}
}
public static void main(String[] args) {
BigOuter.Nest n = new BigOuter.Nest(); // both class names
n.go();
B2 b2 = new B2(); // access the enclosed class
b2.goB2();
}
}

Which produces:
hihi 2

Just as a static method does not have access to the instance variables and non-static methods of the class, a static nested class does not have access to the instance variables and non-static methods of the outer class.

Look for static nested classes with code that behaves like a nonstatic (regular inner) class.

Search Amazon for Best Books on Java J2EE

Blogarama

blogarama - the blog directory

Search your favourite topics

Google