Lecture 7
Nested Classes
Outcome of the Lecture
Upon completion of this lecture you will be able to
Understand the concept of nested classes
Use the different types of nested classes
Outline of the Presentation
Nested Classes
Inner Classes
Local Inner Class
Anonymous Class
Static Nested Classes
Nested Classes
Class within class
a way of logically grouping classes that are only used in one place
increases encapsulation
more readable and maintainable code Nested Classes
class OuterClass { Non static
Static Nested
... Nested Classes
Classes
static class StaticNestedClass { (Inner Classes)
... Inner Classes
} as data
class InnerClass { member
... Local Inner
} Classes
}
Anonymous
Classes
Inner Classes
An inner class is associated with an instance of its enclosing class
has direct access to that object's methods and fields
it cannot define any static members itself
Objects that are instances of an inner class exist within an instance of the
outer class
To instantiate an inner class, you must first instantiate the outer class
class OuterClass {
...
class InnerClass {
...
}
}
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Inner Classes
class Outer { class Outer {
int outer_x = 100; int outer_x = 100;
void test() { class Inner {
Inner inner = new Inner(); void display() {
inner.display(); System.out.println("display: outer_x = " + outer_x);
} }
class Inner { }
void display() { }
System.out.println("display: outer_x = " + outer_x); public class InnerClassDemo {
} public static void main(String args[]) {
} Outer outer = new Outer();
} Outer.Inner obj = outer.new Inner();
class InnerClassDemo { obj.display();
public static void main(String args[]) { }
Outer outer = new Outer(); }
outer.test();
} Can be used in single line as
} Outer.Inner obj = new Outer().new Inner();
Local Inner Classes
Inner class defined within a block as a local member
public class LocalDemo{
void show(){
System.out.println("Outer is invoking inner show() method");
class LocalInner{
void showLocal(){
System.out.println("Local");
}
}
LocalInner localinner = new LocalInner();
localinner.showLocal();
}
public static void main(String ravi[]){
LocalDemo l = new LocalDemo();
l.show();
}
}
Anonymous Inner Classes
Enable you to declare and instantiate a class at the same time
local classes except that they do not have a name
Can be used only once
Make code more concise
Anonymous classes are often used in graphical user interface
(GUI) applications
(will be discussed in details in Event Handling)
Static Nested Classes
As with class methods and variables, a static nested class is
associated with its outer class
Static nested class cannot refer directly to instance variables
or methods defined in its enclosing class
It can use them only through an object reference
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
Static Nested Classes
class Outer {
int outer_x = 100;
static int outer_y = 200;
static class Inner {
void display() {
Outer o = new Outer();
System.out.println("display: outer_x = " +o.outer_x);
System.out.println("display: outer_x = " + outer_y);
}
}
}
public class StaticClassDemo {
public static void main(String args[]) {
Outer.Inner obj = new Outer.Inner();
obj.display();
}
}