Saturday 3 August 2013

25:inner classes, static nested classes, and, anonymous classes

Learning Objectives
After completing this session, you will be able to:
‰  Identify inner classes, static nested classes, and, anonymous classes
Inner Class
‰  Inner class is a class declared within another class
‰  Accessing the members of the inner class:
‰  Need to instantiate an object instance of an inner class first
‰  Example:
innerObj.innerMember = 5;
//innerObj is an instance of the inner class
//innerMember is a member of the inner class
Accessing Members of Outer Class Within an Inner Class
Methods of the inner class can directlyaccess members of the outer class:
Example:
1 class Out {
2 int outData;
3 class In {
4 void inMeth() {
5 outData = 10;
6 }
7 }
8 }
Java Program Structure: Inner Classes
1 class OuterClass {
2 int data = 5;
3 class InnerClass {
4 int data2 = 10;
5 void method() {
6 System.out.println(data);
7 System.out.println(data2);
8 }
9 }
9 public static void main(String args[]) {
10 OuterClass oc = new OuterClass();
11 InnerClass ic = oc.new InnerClass();
12 System.out.println(oc.data);
13 System.out.println(ic.data2);
14 ic.method();
15 }
16 }
Static Nested Classes
‰  A static nested class is just a class enclosed within another class, and marked with the
keyword static.
‰  Static nested classes are considered a member of the enclosing or outer class.
‰  Static nested classes can access only the private static members of the outer class.
‰  Static nested class is not connected to an instance of the outer class.
‰  Static nested class cannot access the variables that are not static and methods of the
outer class.
Static Nested Class: Example
public class OuterClass {
static class InnerClass
void sayIt() {
System.out.println(“method of a static inner class”);
}
}
}
class Test {
public static void main(String[] args) {
OuterClass.InnerClass inner = new OuterClass.InnerClass();
inner.sayIt();
}
}
Nested Classes versus Inner Classes
‰  Any Java class that is defined within the scope of another class is known as a nested
class.
‰  It does not matter if it is anonymous, static, normal, or whatever.
‰  If it is inside another class, then it is technically considered as a nested class.
‰  Nested classes that are not static, often referred to as inner classes.
‰  All inner classes are nested classes, but not all nested classes are inner classes.
Anonymous Classes
‰  It is common in Java programming to encounter situations where you need to create
an object but do not need to bother giving it an explicit name.
‰  With the inner classes you can take this to another level by creating and instantiating a
class without bothering to give it a name. This is called as an anonymous class.
‰  This anonymity eliminates a lot unnecessary named objects and makes the code more
readable.
Anonymous Inner Class: Example
In the following code an instance of the ActionListener class is created in the argument of the
addActionListener method:
..
public class AnOuterClass extends Applet
{
Button fBt = new Button("OK");
public AnOuterClass ()
{
int i = 0;
fBt.addActionListener
( // The argument is the object created by the following:
new ActionListener () // no name given to this object
{
public void actionPerformed (ActionEvent e) {
i++;
System.out.println ("Pressed "+i+" times");
}
}
);
add(fBt);
}
} // class AnOuterClass
Anonymous Inner Class: Example
In this example, in one step you have created an implementation of the ActionListener interface
and created an instance of it for application by the button.
Now the compiler will create a class file name AnOuterClass$1.class where a number, in this case
"1", is used to identify the class files for anonymous inner classes.
Code:
class Outer {
String s = "Outer";
public static void main(String[] args) {
new Outer().new Inner();
}
class Inner {
String s = "Inner";
Inner() {
System.out.println(this.s);
System.out.println(Outer.this.s);
}
}
}
Static Nested Classes
‰  Static nested classes are inner classes marked with the static modifier.
‰  A static nested class is not an inner class; it’s a top-level nested class.
‰  You don’t need an instance of the outer class to instantiate a static nested class.
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.
‰  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.
‰  An anonymous inner class can extend one subclass or implement one interface.
‰  An anonymous-local inner class is declared,defined, and automatically instantiated as
part of a method invocation.

No comments:

Post a Comment