Refer to Java - Inner classes
Writing a class within another class is allowed in java. The class written within is called a
nested class
, and the class holding the inner class is called theouter class
.
Nested class
- Non-static nested class
- Inner class
- Method-local inner class
- Anonymous inner class
- Static nested class
Inner class
class OuterDemo01 {
private class InnerDemo {
private void print() {
System.out.println("This is an inner class.");
}
}
void displayInner() {
InnerDemo inner = new InnerDemo();
inner.print();
}
}
public class TestNested {
public static void main(String[] args) {
OuterDemo01 outer = new OuterDemo01();
outer.displayInner();
}
}
This is an inner class.
class OuterDemo02 {
private int num = 10;
public class InnerDemo {
public int getNum() {
return num;
}
}
}
public class TestNested02 {
public static void main(String[] args) {
OuterDemo02 outer = new OuterDemo02();
OuterDemo02.InnerDemo inner = outer.new InnerDemo();
System.out.println(inner.getNum());
}
}
10
Method-local inner class
public class OuterDemo03 {
private void myMethod() {
int num = 10;
class MethodLocalDemo {
private void print() {
System.out.println("This is a method-local inner class " + num);
}
}
MethodLocalDemo methodLocal = new MethodLocalDemo();
methodLocal.print();
}
public static void main(String[] args) {
OuterDemo03 outer = new OuterDemo03();
outer.myMethod();
}
}
This is a method-local inner class 10
Anonymous inner class
An inner class declared without a class name is known as an
anonymous class
. In case of anonymous inner classes, we declare and instantiate them at the same time.
Generally they are used to override the methods of a class or an interface.
abstract public class AnonymousInner {
public abstract void myMethod();
}
public class OuterDemo04 {
public static void main(String[] args) {
AnonymousInner inner = new AnonymousInner() {
@Override
public void myMethod() {
System.out.println("This is an example of an anonymous inner class.");
}
};
inner.myMethod();
}
}
This is an example of an anonymous inner class.
Generally, if a method accepts an object of an interface, an abstract class, or a concrete class, then we can implement the interface, extend the abstract class, and pass the object to the method. If it is a class, then we can directly pass it to the method.
interface Message {
String greet();
}
public class ArgumentClassDemo {
public void displayMessage(Message message) {
System.out.println(message.greet() + ", " + "this is an example of an anonymous inner class as an argument.");
}
public static void main(String[] args) {
ArgumentClassDemo argumentDemo = new ArgumentClassDemo();
argumentDemo.displayMessage(new Message() {
@Override
public String greet() {
return "Hello";
}
});
}
}
Hello, this is an example of an anonymous inner class as an argument.
Static nested class
A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static member.Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.
public class OuterDemo05 {
static class NestedDemo {
void show() {
System.out.println("This is the method from my static nested class.");
}
}
public static void main(String[] args) {
OuterDemo05.NestedDemo nested = new OuterDemo05.NestedDemo();
nested.show();
}
}
This is the method from my static nested class.