Tuesday, 19 December 2006

SCJP Chapter 8

Inner Classes
There are four types of inner classes, your 'regular joe' inner class, method-local inner class, anonymous inner class and the static nested class (which is not really an inner class at all).

Regular Inner Class
Beginning with the regular inner class, which is coded alongside the outer class's variable and method members, one needs to understand that the inner class has access to all of the outer class members. Even private ones. The outer class needs to create an instance of the inner class before it can begin accessing the inner class members. Also when calling from another class, the outer class need to be instantiated before one can instantiate the inner class:

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

When in the inner class, the object self-reference is this. The outer class reference is NameofOuterClass.this. The same modifiers (final, abstract, public, private, protected, strictfp and static - though that changes it into a static nested class) that can be applied to instance variables and methods can also be applied to inner classes.

Method-Local Inner Class

An inner class defined within a method, it requires instantiation (below the class definition) inside the method to be available for use. No other method or class can create the method-local inner class aside from the method which holds that class. Like regular inner classes, it can access the outer class members, however it cannot access the local variables inside the method containing it unless these variables are marked final. Local variables exist on the stack and within the method lifetime only. Only two modifiers for the method-local inner class, abstract or final.

Anonymous Inner Class
Nameless inner classes with two versions, regular and argument-defined.

The regular version has two flavors, creating a subclass of any class type and creating a class implementing an interface. Subclassing is used to override the methods of a superclass, however creating a new method is legal though there is no way to invoke it short of a compilation error. The syntax for both flavors is unusual:

class Popcorn {
public void pop() {

interface Cookable{
public void cook();

class Food {
Popcorn p = new Popcorn(){
public void pop(){
System.out.println("anonymous popcorn");} };

Cookable c = new Cookable(){
public void cook(){
System.out.println("anonymous cookable implementor");} };

Note the semicolon indicating the end of the statement. The semicolon is not present in argument-defined anonymous inner classes as the definition is located in the argument portion of the method call.

Static Nested Classes
A class that is simply a static member of the enclosing class. Meaning it can be accessed without instantiating the enclosing class. Naturally being static, it can only access other static variables and methods. Syntax for it:

MyOuter.StaticInner sinner = new MyOuter.StaticInner();

Or within the enclosing class, MyOuter:

StaticInner sinner = new StaticInner();

No comments: