Wednesday, December 04, 2013

Types of Inner Classes in Java

Static Inner Classes:

Syntax for static inner class is as follows:
<access-specifier> class OuterClassName {
    public static class <StaticInnerClassName> {
        . . .
    }
    . . .
}



for static inner classes following additional properties hold:
  • Static members of the outer class are visible to the static inner class, what ever their access level be.
  • Non-static members of the outer class are not available, because there is not instance of the outer class.
  • An inner class may not have static members unless the inner class is itself marked as static.
  • Sometimes static nested class are not refered to as inner class at all, as they don’t require outer classes instance.
  • A static inner class is just like any other inner class, but it dose not have the reference to its outer class object that generated it.
There are two more types of inner classes, i.e local inner classes & anonymous inner classes. The local inner class are defined within a method. Anonymous inner classes are also defined with in a method but have no name.

Local Inner Classes:

Syntax of the local inner class is as follows:
<access-specifier> class <OuterClassName> {
    code...
    <access-specifier> <return-type> <MethodName>(<arguments>){
        class <LocalInnerClassName>{
            code...
        }
        code...
    }
    code...
}
  • Local classes are never declared with an access specifier (that is, public or private). Their scope is always restricted to the block in which they are declared.
  • Local classes have a great advantage: they are completely hidden from the outside world.
  • They can not only access the instance variables but local variables of the method (in which they are defined) as well, but the local varible has to be declared final.

Anonymous Inner Classes

When using local inner classes, you can often go a step further. If you want to make only a single object of this class, you don’t even need to give the class a name.
Such a class is called an anonymous inner class. Usually the inner class extend some interface or extend other class.
This syntax for anonymous classes is very cryptic.
new SuperType(construction parameters) {
    inner class methods and data
}
  • Here, SuperType can be an interface, such as ActionListener; then, the inner class implements that interface. Or SuperType can be a class; then, the inner class extends that class.
  • An anonymous inner class cannot have constructors because the name of a constructor must be the same as the name of a class, and the class has no name. Instead, the construction parameters are given to the superclass constructor. In particular, whenever an inner class implements an interface, it cannot have any construction parameters. Nevertheless, you must supply a set of parentheses as in
new InterfaceType () { methods and data }
  • It is recommended to refrain from using them as many programmers find too many anonymous classes hard to read.

No comments:

Post a Comment

Distributed Transactions

What is a distributed transaction?  Transactions that span over multiple physical systems or computers over the network, are simply termed D...