1. Inner Classes
In Java, just like methods and variables, a class can also have another class as its member. Writing a class within another class is allowed in Java.
The class written within another class is called the nested class, while the class that contains it is called the outer class.
Syntax
class Outer_Demo {
class Inner_Demo {
}
}
Nested Classes
Nested classes are divided into two broad categories:
- Non-static nested classes — non-static members of a class.
- Static nested classes — static members of a class.
Inner Classes
Inner classes are non-static nested classes. They can be used as a mechanism for encapsulation and can access members of the enclosing class.
2. Types of Nested / Inner Classes
Depending on how and where they are defined, inner classes are commonly discussed as:
Inner Class
A non-static class declared as a member of another class.
Method-local Inner Class
A class declared inside a method; its scope is restricted to that method.
Anonymous Inner Class
A class declared without a class name, normally created and instantiated at the same time.
Static Nested Class
A static member class that can be accessed without creating an instance of the outer class.
| Type | Main characteristic |
|---|---|
| Inner class | Non-static member of an outer class. |
| Method-local inner class | Declared inside a method and limited to that method. |
| Anonymous inner class | No class name; declaration and instantiation occur together. |
| Static nested class | Static member of the outer class. |
3. Inner Class (Non-static Nested Class)
An inner class is a class written inside another class. Unlike a top-level class, an inner class can be declared with the private access modifier.
When an inner class is private, it cannot be accessed directly from an object outside the outer class. The outer class can expose its behavior through a method.
Example: Private Inner Class
class Outer_Demo {
int num;
private class Inner_Demo {
public void print() {
System.out.println("This is an inner class");
}
}
void display_Inner() {
Inner_Demo inner = new Inner_Demo();
inner.print();
}
}
public class PrivateInnerClass {
public static void main(String args[]) {
Outer_Demo outer = new Outer_Demo();
outer.display_Inner();
}
}
Creating an Inner Class Object
The PDF also demonstrates that an inner class can be instantiated using an instance of the outer class.
Outer_Demo outer = new Outer_Demo(); Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
4. Method-local Inner Class
In Java, a class can be declared inside a method. Such a class is a method-local inner class.
Like local variables, the scope of the method-local inner class is restricted to the method in which it is defined.
- It is declared inside a method.
- Its scope is limited to that method.
- It can be instantiated only inside the method where it is defined.
- It can access members available in its surrounding context subject to Java's local-class rules.
Example
public class MethodLocalInnerClass {
int a;
void my_Method() {
int num = 23;
class MethodInner_Demo {
public void print() {
System.out.println("This is method inner class " + num);
System.out.println("This is method inner class " + a);
}
}
MethodInner_Demo inner = new MethodInner_Demo();
inner.print();
}
public static void main(String args[]) {
MethodLocalInnerClass outer =
new MethodLocalInnerClass();
outer.my_Method();
}
}
5. Anonymous Inner Class
An inner class declared without a class name is known as an anonymous inner class.
In an anonymous inner class, the class is declared and instantiated at the same time. It is commonly used when you need to override a method of a class or interface for a specific use.
Syntax
AnonymousInner an_inner = new AnonymousInner() {
public void my_method() {
........
........
}
};
6. Anonymous Inner Class with an Abstract Class
An anonymous inner class can provide implementations for the abstract methods of an abstract class.
Example
public abstract class AbstractDemo {
public abstract void myMessage();
public abstract void display();
public static void main(String[] args) {
AbstractDemo obj = new AbstractDemo() {
public void myMessage() {
System.out.println(
"Hi I am from Abstract method Welcome to Java!!");
}
@Override
public void display() {
System.out.println(
"Hey I am from anonymous inner classes");
}
};
obj.myMessage();
obj.display();
}
}
The anonymous object provides concrete implementations of both abstract methods without creating a separate named subclass.
7. Anonymous Inner Class with an Interface
An anonymous inner class can also implement an interface directly.
Interface
public interface InterfaceDemo {
public abstract void myMessage();
public abstract void display();
}
Implementation Using Anonymous Inner Class
public class InterfaceDemoImpl {
public static void main(String[] args) {
InterfaceDemo obj = new InterfaceDemo() {
public void myMessage() {
System.out.println(
"Hi I am from Abstract method Welcome to Java!!");
}
@Override
public void display() {
System.out.println(
"Hey I am from anonymous inner classes");
}
};
obj.myMessage();
obj.display();
}
}
8. Anonymous Inner Class as an Argument
An anonymous inner class can be passed directly as an argument to a method that accepts an interface type.
Example
interface Message {
String greet();
}
public class My_class {
public void displayMessage(Message m) {
System.out.println(
m.greet() +
", This is an example of anonymous inner class as an argument");
}
public static void main(String args[]) {
My_class obj = new My_class();
obj.displayMessage(new Message() {
public String greet() {
return "Hello";
}
});
}
}
How It Works
Messagedefines thegreet()contract.displayMessage()accepts aMessageobject.- An anonymous implementation of
Messageis created directly inside the method call. - The anonymous object implements
greet()and returns"Hello".
9. Static Nested Class
A static nested class is a nested class that is declared as a static member of the outer class.
It can be accessed without creating an instance of the outer class.
Syntax
class MyOuter {
static class Nested_Demo {
}
}
Example
public class Outer {
static class Nested_Demo {
public void my_method() {
System.out.println("This is my nested class");
}
}
public static void main(String args[]) {
Outer.Nested_Demo nested =
new Outer.Nested_Demo();
nested.my_method();
}
}
Instantiation
Outer.Nested_Demo nested =
new Outer.Nested_Demo();
This is different from a non-static inner class, where an instance of the outer class is required before creating the inner-class object.
10. Quick Revision
| Concept | Key Point |
|---|---|
| Nested Class | A class declared inside another class. |
| Outer Class | The class that contains the nested class. |
| Inner Class | A non-static nested class. |
| Method-local Inner Class | Declared inside a method and scoped to that method. |
| Anonymous Inner Class | Class without a name; declaration and instantiation happen together. |
| Static Nested Class | A static member class that can be accessed without an outer-class instance. |
| Private Inner Class | Can be declared private because it is a member of the outer class. |
| Anonymous with Abstract Class | Provides implementations for abstract methods inline. |
| Anonymous with Interface | Provides an implementation of an interface inline. |
Inner Class Creation vs Static Nested Class
| Non-static Inner Class | Static Nested Class |
|---|---|
| Requires an instance of the outer class for normal instantiation. | Does not require an outer-class instance. |
| Can access instance members of the outer class. | Cannot directly access instance members of the outer class. |
Example: outer.new Inner_Demo() |
Example: new Outer.Nested_Demo() |
Key Points to Remember
- A class can be declared inside another class.
- Non-static nested classes are called inner classes.
- Inner classes can access members of their enclosing class.
- A method-local class exists only within the method where it is declared.
- An anonymous class has no class name and is created inline.
- Anonymous classes can extend classes or implement interfaces.
- A static nested class is a static member of the outer class.
- A static nested class is accessed using
Outer.Nested. - A static nested class cannot directly access instance members of the outer class.