-
Notifications
You must be signed in to change notification settings - Fork 0
Module 2: Inner Classes
Java inner class or nested class is a class that is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable.Private methods of the outer class can be accessed, so bringing a new dimension and making it closer to the real world.
There are four types of inner classes in Java:
A member inner class is a class that is defined within another class. It is a non-static class. It can access all the members of the outer class including private data members and methods. It can also be accessed by any other class within the package. It cannot access local variables of a method unless they are declared final.
public class OuterClass {
private int data = 30;
class InnerClass{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
OuterClass obj = new OuterClass();
OuterClass.InnerClass in = obj.new InnerClass();
in.msg();
}
} A local inner class is a class that is defined within a method. It is a non-static class. It cannot access any non-final local variables of the enclosing method. It can access all the members of the enclosing class. It cannot be accessed from outside the method.
public class OuterClass{
private int data=30;
void display(){
class Local{
void msg(){System.out.println(data);}
}
Local l=new Local();
l.msg();
}
public static void main(String args[]){
OuterClass obj=new OuterClass();
obj.display();
}
}An anonymous inner class is a class that has no name. It is created for implementing interfaces and extending classes but has no use after creating an object of it. It is created using new keyword. It can be created only inside a method and cannot be declared inside a class or interface. It cannot have a constructor. It can access all the members of the enclosing class including private data members and methods. It cannot access local variables of a method unless they are declared final.
interface Message{
void msg();
}
public class TestAnonymousInner{
public static void main(String args[]){
Message m = new Message(){
public void msg(){
System.out.println("Hello anonymous inner class");
}
};
m.msg();
}
}A static nested class is a class that is defined within another class with the static keyword. It cannot access non-static data members and methods. It can be accessed using the outer class name. It cannot access non-static (instance) data members or methods. It can access static data members of outer class including private. It can be accessed by other classes by outer class name. It cannot access non-static (instance) data members or methods. It can access static data members of outer class including private. It can be accessed by other classes by outer class name.
class OuterClass{
static int data=30;
static class Inner{
void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
OuterClass.Inner obj=new OuterClass.Inner();
obj.msg();
}
}