Mar 6, 2009

Java - Inner Classes - for SCJP

If anonymous class is created for interface, it extends Object class and implement that interface, if it is created for a class then it extends that class.
Earlier, Non-static inner classes were not allowed to have static fields at all. This rule is now modified and the new rule says that: 'The third paragraph of the section Members that can be marked static is amended to make an exception and allow inner classes to declare static FIINAL fields that are compile time constants as members.'
Ex.:
public class Outer{
class Inner
{
static final int k = 10;
}
}
(Top level classes mean, classes defined in package scope and STATIC inner classes of classes defined in package scope) Consider the anonymous inner class for catching action events:

ActionListener al = new ActionListener(){
public void actionPerformed(ActionEvent e)
{
}

};

Here, the anonymous class implicitly extends Object and implements the interface ActionListener. Explicit extends or implements clauses are not allowed for such classes. Other inner class (ie. non anonymous) can have them. Consider the following (although of no use) class:
public class TestClass
{
public TestClass(int i) { }

public void m1()
{

TestClass al = new TestClass(10)

{

public void actionPerformed(ActionEvent e)
{
}

};
}

}
This illustrates 4 points:
  1. Instance Methods can also have inner classes. (But they cannot have static inner classes).

  2. Inner class can extend the outer class.

  3. Anonymous inner class can be created for classes. (Not just for interfaces). They implicitly extend the class.(Here, TestClass)

  4. Anonymous inner class can have initialization parameter. (If the class they extend has a corresponding constrctor).

Non-static inner classes can contain final static fields (but not methods).
Anonymous classes cannot have explict constructors, since they have no names.
A static inner class is also known as A Top Level Nested class. So,there are two types of Top level classes. One, that is a standard class and other an inner class which is static.
Eg.
public class A //This is a standard Top Level class.
{
class X

{

static final int j = 10; //compiles fine!

}

public static class B //This is also a Top Level class (but nested!)

{
}

}
You can create objects of B with having objects of A. Eg. A.B b = new A.B();
Members in outer instances are directly accessible using simple names. There is no restriction that member variables in inner classes must be final.
Nested classes define distinct types from the enclosing class, and the instanceof operator does not take of the outer instance into consideration.

Every non static inner class object has a reference to it's out class object which can be accessed by doing OuterClass.this. So the expression B.this.c will refer to B's c, which is 'a'. Inside a non-static inner class, 'InnerClass.this' is equivalent to 'this'. so 'C.this.c' refers to C's c which is 'c'. The expression super.c will access the variable from A, the superclass of C which is 'd'.

Only classes declared as members of top-level classes can be declared static. Such a member is a top-level nested class if it is declared static, otherwise it is a non-static inner class.
Package member classes, local classes(ie. classes declared in methods) and anonymous classes cannot be declared static.

No comments: