INNER Class
INNER Class
Sometimes we can declare a class inside another class such type of classes is called Inner Classes.
Sun people introduced inner classes in 1.1 version as part of "Event Handling" to resolve GUI bugs.
But because of powerful features and benefits of inner classes slowly the programmers start using in
regular coding also.
Without existing one type of objects if there is no chance of existing another type of object then we
should go for inner classes.
Ex.: - Without existing University object there is no chance of existing Department object hence we
have to define Department class inside University class.
NOTE :- *Without existing Outer Class there is no chance of existing Inner class objects.
*The relationship between outer class and inner class is not IS-A relationship and it is Has-A
relationship.
Based on the purpose and position of declaration all inner classes are divided into 4
types.
They are:
1. Normal or Regular inner classes
2. Method Local inner classes
3. Anonymous inner classes
4. Static nested classes.
1. Normal or Regular Inner Class
If we are declaring any named class inside another class directly without static modifier such type of inner
classes are called normal or regular inner classes.
Example: class Outer {
class Inner {
}
}
If we are declaring inner class inside instance method then we can access both static and non static
members of outer class directly.
But if we are declaring inner class inside static method then we can access only static members of outer
class directly and we can't access instance members directly.
From method local inner class we can't access local variables of the method in which we declared it.
But if that local variable is declared as final then we won't get any compile time error
class Test {
int x=10;
public void methodOne() {
int y=20;
class Inner {
public void methodTwo() {
System.out.println(x);
System.out.println(y);
}
}
Inner i=new Inner();
i.methodTwo();
}
public static void main(String[] args) {
new Test().methodOne();
}
}
3. Anonymous Inner Classes
Sometimes we can declare inner class without name such type of inner classes are called anonymous
inner classes.
The main objective of anonymous inner classes is "just for instant use".
classPopCorn {
public void taste() {
System.out.println("spicy");
}
}
class Test {
public static void main(String[] args) {
PopCorn p=new PopCorn() {
public void taste() {
System.out.println("salty");
}
};
p.taste();//salty
PopCorn p1=new PopCorn()
p1.taste();//spicy
}
}
Note: Inside Anonymous inner classes we can take or declare new methods but outside of anonymous
inner classes we can't call these methods directly because we are depending on parent reference. [parent
reference can be used to hold child class object but by using that reference we can't call child specific
methods]. These methods just for internal purpose only.
class Test {
static class Nested{
public void methodOne() {
System.out.println("nested class method");
}
}
public static void main(String[] args){
Test.Nested t=new Test.Nested();
t.methodOne();
}
}
Inside static nested classes we can declare static members including main() method also. Hence it is
possible to invoke static nested class directly from the command prompt.
From the normal inner class we can access both static and non static members of outer class but from
static nested class we can access only static members of outer class.