All About Inner Class in Java

All About Inner Class in Java

6 mins read746 Views Comment
Updated on Jun 26, 2023 17:29 IST

Uncover the intricacies of inner classes in Java with our extensive guide. From basic concepts to practical examples, learn how to define, instantiate, and use inner classes effectively. Ideal for Java developers of all levels, seeking to improve their object-oriented programming skills and code structure.

2023_05_Feature-Image-Templates-24.jpg

The class defined inside the other class is called Inner Class in Java. The inner class concept was introduced in Java 1.1 to fix the bugs in GUI as part of event handling. Still, because of powerful concepts and features, programmers have started using inner-class concepts in regular coding.
If there is no chance of existing another type of object, we should go for inner classes without existing one type of object.

Must Check: Top Free Online Java Courses and Certifications

Must Check: Top Online Java Courses and Certifications

Syntax of Inner Class in Java

 
class OuterClass{
//code
class InnerClass{
//code
}
}
Copy code

Example:
Let’s take the example of a university, so we have a department in university. If this University gets closed or does not exist, then the Department is also closed and not exist. Without an existing University class object, there is no chance of an existing Department class object; in this case, we should go for the inner class concept. Then, we declare these scenarios like below.

 
class University{
//code
class Department{
//code
}
}
Copy code

Types of Inner Class in Java

The inner class is divided into four types based on position and behavior.

Normal or Regular Inner Classes

If we are declaring a named class inside another class without static modifier, such type of class in normal or regular inner class.

Code:

 
//outer class
public class University {
private int courseId;
// regular inner class
class Department{
// inner class method
public void computer(){
System.out.println("We are Learning Java");
//accessing private variable of outer class University
courseId=1001;
System.out.println("Course Id for Java is:"+ courseId);
}
}
public static void main(String[] args) {
//creating outer class instance
University university=new University();
//creating instance of regular inner class
University.Department depart=university.new Department();
//calling inner class method
depart.computer();
}
}
Copy code
2023_05_Normal-or-regular-Inner-classes.jpg
Difference Between List and Set
All About indexOf() Method of String Class in Java
How to Generate Random Numbers in Java?

Method Local Inner Class

Declaring the class inside the method of the outer class, then that class is considered as method local inner class. The scope of this class is limited to that particular method inside which it is declared. The method’s local inner class can be instantiated only within the method where the inner class is declared. Observe the below code to understand this concept.
In the below program, we declare the inner class Department inside the getDepartment() method of the outer class University. In the inner class Department, we have created a method called a computer(); inside this method, we are accessing and assigning the outer class’s private variable courseId and private static variable courseFee variables. In the getDepartment() method, we have created the object of the Department class and called the computer() method. Inside the main method, we have created the object of the University class and called the getDepartment() method. Since the scope of the inner class is limited to that particular method and can access the members of the outer class, we get the expected output.

 
//outer class
public class University {
private int courseId;
private static double courseFee;
public void getDepartment() {
// method local inner class
class Department{
// inner class method
public void computer(){
double TutionFee=20000.00;
int extraFee=1000;
System.out.println("We are Learning Java");
//accessing private variable of outer class University
courseId=1001;
System.out.println("Course Id for Java is:"+ courseId);
//accessing the static variable of outer class
courseFee=TutionFee+extraFee;
System.out.println("CourseFee:"+ courseFee);
}
}
//creating instance of inner class in inside the method of outer class
Department depart=new Department();
//calling method of inner class
depart.computer();
}
public static void main(String[] args) {
//creating outer class instance
University university=new University();
//calling outer class method
university.getDepartment();
}
}
Copy code

Output

2023_05_Method-local-inner-class.jpg

Explore Free Java Courses

Anonymous Inner Class

The inner class without a name is considered an Anonymous inner class, and only a single object is created for the Anonymous inner class. We use the Anonymous inner class when we need to override the method of a class or interface method. The main purpose of the anonymous inner class is just for instant use (one-time use). There are three types of anonymous inner class.

  • The anonymous inner class that extends a class
  • An anonymous inner class that implements an interface
  • An anonymous inner class that defines inside arguments

Anonymous Inner Class that Extends a Class

In the program below, we created an abstract class, Player, with the abstract method of play(). An anonymous class is created inside the main method, but the compiler decides its name. This anonymous class is created with reference variable p of Player type and implements the play() method.

 
abstract class Player{
abstract void play();
}
public class Main {
public static void main(String[] args) {
// object of anonymous class is created with parent reference Player
Player p=new Player() {
//implementation of play() method
void play() {
System.out.println("Playing Football !");
}
};
p.play();
}
}
Copy code

Output:

2023_05_Anonymous-inner-class-that-extends-a-class.jpg

The Anonymous Inner Class that Defines Inside Interface

In the program below, we created an interface Player and declared a method play. An anonymous class is created inside a main method, but the compiler decides its name. This anonymous class implements the interface Player and provides the implementation of the play() method.

Code

 
interface Player{
void play();
}
public class Main {
public static void main(String[] args) {
// object of anonymous class is created with parent reference Player
Player p=new Player() {
//implementation of play() method
public void play() {
System.out.println("Playing Football !");
}
};
p.play();
}
}
Copy code

Output

2023_05_Anonymous-inner-class-that-extends-a-class-1.jpg

Anonymous Inner Class that Define Inside Arguments

Anonymous Inner can be defined inside as an argument in the method or the constructor; let’s see the program where a thread is created using this type of anonymous inner class.

Code:

 
public class Main {
public static void main(String[] args) {
//anonymous inner class that define inside the argument
Thread thread=new Thread(new Runnable() {
public void run() {
System.out.println(" child Thread running");
}
});
thread.start();
System.out.println(" Main Thread running");
}
}
Copy code

Output

2023_05_Anonymous-Inner-Class-that-Define-Inside-Arguments-1.jpg

Static Nested Class

We use the keyword static to make the nested class static. In Java, only nested classes are allowed to be static. Static nested classes are technically not an inner class. We have two cases in the static nested class:

Instance Member

When instance methods or variables are present inside the static nested class, we must create the object of the static nested class. We do not have to create an object of the outer class because the nested class is static and static properties and members are accessed without an object.

In the below program, instance method display() is accessed with the help of an Inner class object.

Code

 
public class Main {
static int value=500;
static class Inner{
public void display() {
System.out.println("The value is:"+ value);
}
}
public static void main(String[] args) {
//creating object of static nested Inner class
Main.Inner obj= new Main.Inner();
obj.display();
}
}
Copy code

Output:

2023_05_Static-Nested-Class-1.jpg

Static Member

When we have static methods or variables inside the static nested class, there is no need to create the object of the static class. Observe the below program.

Code:

 
public class Main {
static int value=500;
static class Inner{
public static void display() {
System.out.println("The value is:"+ value);
}
}
public static void main(String[] args) {
// no need to create the object of static nested class Inner
Main.Inner.display(); }
}
Copy code

Output:

2023_05_Static-nested-class-with-static-member.jpg

Contributed By: Shubham Kumar

FAQs

Can an inner class access members of other instances of the enclosing class?

Yes, an inner class has access to the members of all instances of the enclosing class. It can access these members directly or using the this keyword followed by the member name.

Can an inner class be declared as static?

Yes, an inner class can be declared as static. A static inner class does not have access to the instance members of the enclosing class and can be instantiated without an instance of the enclosing class.

What is an inner class in Java?

An inner class, also known as a nested class, is a class that is defined within another class. It has access to the members (fields, methods) of its enclosing class and can be used to logically group related functionality.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio