Java - Inner Classes
Java - Inner Classes
Nested Classes
In Java, just like methods, variables of a class too can have another class as its member. Writing
a class within another is allowed in Java. The class written within is called the nested class, and
the class that holds the inner class is called the outer class.
Syntax
Following is the syntax to write a nested class. Here, the class Outer_Demo is the outer class
and the class Inner_Demo is the nested class.
class Outer_Demo {
class Inner_Demo {
Inner classes are of three types depending on how and where you define them. They are −
https://www.tutorialspoint.com/java/java_innerclasses.htm 1/9
15/04/2022, 16:16 Java - Inner classes
Inner Class
Method-local Inner Class
Anonymous Inner Class
Inner Class
Creating an inner class is quite simple. You just need to write a class within a class. Unlike a
class, an inner class can be private and once you declare an inner class private, it cannot be
accessed from an object outside the class.
Following is the program to create an inner class and access it. In the given example, we make
the inner class private and access the class through a method.
Example
Live Demo
class Outer_Demo {
int num;
// inner class
void display_Inner() {
inner.print();
outer.display_Inner();
Here you can observe that Outer_Demo is the outer class, Inner_Demo is the inner class,
display_Inner() is the method inside which we are instantiating the inner class, and this method
is invoked from the main method.
If you compile and execute the above program, you will get the following result −
https://www.tutorialspoint.com/java/java_innerclasses.htm 2/9
15/04/2022, 16:16 Java - Inner classes
Output
As mentioned earlier, inner classes are also used to access the private members of a class.
Suppose, a class is having private members to access them. Write an inner class in it, return the
private members from a method within the inner class, say, getValue(), and finally from another
class (from which you want to access the private members) call the getValue() method of the
inner class.
To instantiate the inner class, initially you have to instantiate the outer class. Thereafter, using
the object of the outer class, following is the way in which you can instantiate the inner class.
The following program shows how to access the private members of a class using inner class.
Example
Live Demo
class Outer_Demo {
// inner class
System.out.println(inner.getNum());
https://www.tutorialspoint.com/java/java_innerclasses.htm 3/9
15/04/2022, 16:16 Java - Inner classes
If you compile and execute the above program, you will get the following result −
Output
In Java, we can write a class within a method and this will be a local type. Like local variables,
the scope of the inner class is restricted within the method.
A method-local inner class can be instantiated only within the method where the inner class is
defined. The following program shows how to use a method-local inner class.
Example
Live Demo
public class Outerclass {
void my_Method() {
class MethodInner_Demo {
inner.print();
outer.my_Method();
If you compile and execute the above program, you will get the following result −
Output
https://www.tutorialspoint.com/java/java_innerclasses.htm 4/9
15/04/2022, 16:16 Java - Inner classes
An inner class declared without a class name is known as an anonymous inner class. In case
of anonymous inner classes, we declare and instantiate them at the same time. Generally, they
are used whenever you need to override the method of a class or an interface. The syntax of an
anonymous inner class is as follows −
Syntax
........
........
};
The following program shows how to override the method of a class using anonymous inner
class.
Example
Live Demo
abstract class AnonymousInner {
};
inner.mymethod();
If you compile and execute the above program, you will get the following result −
Output
In the same way, you can override the methods of the concrete class as well as the interface
using an anonymous inner class.
https://www.tutorialspoint.com/java/java_innerclasses.htm 5/9
15/04/2022, 16:16 Java - Inner classes
obj.my_Method(new My_Class() {
.....
.....
});
The following program shows how to pass an anonymous inner class as a method argument.
Example
Live Demo
// interface
interface Message {
String greet();
obj.displayMessage(new Message() {
return "Hello";
});
If you compile and execute the above program, it gives you the following result −
https://www.tutorialspoint.com/java/java_innerclasses.htm 6/9
15/04/2022, 16:16 Java - Inner classes
Output
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 members. Just like static
members, a static nested class does not have access to the instance variables and methods of
the outer class. The syntax of static nested class is as follows −
Syntax
class MyOuter {
Instantiating a static nested class is a bit different from instantiating an inner class. The following
program shows how to use a static nested class.
Example
Live Demo
public class Outer {
nested.my_method();
}
If you compile and execute the above program, you will get the following result −
Output
https://www.tutorialspoint.com/java/java_innerclasses.htm 7/9
15/04/2022, 16:16 Java - Inner classes
Video
16 Lectures 2 hours
Malhar Lathkar
More Detail
Video
19 Lectures 5 hours
Malhar Lathkar
More Detail
Video
Anadi Sharma
More Detail
Video
https://www.tutorialspoint.com/java/java_innerclasses.htm 8/9
15/04/2022, 16:16 Java - Inner classes
More Detail
Video
More Detail
Video
76 Lectures 7 hours
Arnab Chakraborty
More Detail
https://www.tutorialspoint.com/java/java_innerclasses.htm 9/9