Inner Class in Java

For Java lovers who are learning it for academic reasons or vocational reasons, this blog will help you understand the inner class in Java and its types.

introduction to Java’s Inner Class

It is possible to nest classes in Java by writing classes inside classes. This arrangement is referred to as nested classes or inner classes. This article will explain the important concepts of inner class in java, including their types, described using syntax and examples.

Inner Class in Java

A nested class or an inner class is a class that exists within another class. In other words, the inner class is a part of a class, just as variables and methods are members of a class; it can also be a member of another class. A top-level class or outer class contains other classes as members. A top-level class can have an unlimited number of inner classes.

Advantages of Java inner classes

  • Nested classes exhibit a specific form of connection in that they may access all of the outer class’s members, even private members.
  • Nested classes create more understandable and maintainable code by logically grouping classes and interfaces in a single location.
  • Code optimization means writing less code.

Need for Inner Class in Java

You must be wondering why an inner class in java is used instead of different classes. The following considerations will help you understand the purpose and significance of utilizing inner class:

  • It assists in the logical classification of classes that belong together.

If a class is only beneficial to one other class, we can logically encapsulate it in that class and keep the two classes together. It will assist in the streamlining of their offering.

  • It assists in encapsulation

Assume there are two top-level or outer classes, A1 and A2, and class A2 requires access to the private members of class A1. Members of A1 can be deemed private by nesting class AA1 within class A1, and A2 can access them.

We can also shield C2 from the outer world. This will eventually result in robust encapsulation and security.

  • It contributes to the code’s readability and maintainability

Placing inner classes into top-level classes brings the code closer to where it will be utilized.

Syntax

The syntax for creating an inner or nested class is as follows. In this case, the outer or top-level class is ClassDemoSession, while the nested or inner class is ClassDemo2.

 

class ClassDemoSession

{

 //code for the outer class

 

 class ClassDemoSession2

 {

 //code for the inner class

 }

}

Difference between nested class and inner class in Java

Inner class: is implicitly linked with the contained class’s enclosing instance, which implies it is allowed to execute methods and access variables of the surrounding instance. An inner class in java is commonly used to define an Adapter class.

  • Inner class has
    1. Member inner class,
    2. Anonymous inner class,
    3. Local inner class.

Static Nested Class: Because the nested class cannot access the surrounding class instance and execute methods on it, it should be used when the nested class does not require access to an instance of the encompassing class. The static nested class is used to build elements of the outer object.

Class Type Description
Member Inner Class created within the class and outside methods.
Anonymous Inner Class Built for the purpose of implementing an interface or extending a class. The Java compiler determines its name.
Local Inner Class created within the method.
Static Nested Class created within the class.
Nested Interface created within an interface or class.

Types of Inner Classes in Java

Four types of inner classes:

  1. Nested Inner Class
  2. Static Inner Class
  3. Method Local Inner Class
  4. Anonymous Inner Class
  • Nested Inner Class

A nested inner class can access the outer class’s instance variables, even if they are specified as private. We can use an access modifier for the nested inner class – public, private, protected, or default.

Code to understand Nested Inner Class:

package com.tech.innerclass;

public class techOuterClass

{

 // private variable

 private int value = 50;

 

 // inner class

 class techInnerClass

 {

 // public variable

 public int getValue()

 {

 System.out.println(“Here we have a getValue method of the inner class:”);

 return value;

 }

 } 

 public static void main(String args[])

 {

 techOuterClass outer = new techOuterClass();

 

 techOuterClass.techInnerClass inner = outer.new techInnerClass();

 System.out.println(“result:” inner.getValue());

 }

}

Output:

Here we have a getValue method of the inner class:

result: 50

  • Method Local Inner class

Method Local Inner Class allows us to define a class of a local type within a method body. The scope of the inner class, like that of the local variables, is limited to the method.

We can only initialize a local inner class within the function that defines the inner class. Method Local Class cannot be declared as private, protected, static, or transitory, it can be declared as abstract and final, but not at the same time.

Code to understand Method Local Inner Class:

package com.tech.innerclass;

 

public class OutClass

{

 void outerMethod()

 {

 System.out.println(“Inside method”);

 

 class InnClass

 {

 void innerMethod()

 {

 System.out.println(“Inside method 2”);

 }

 } 

 

 InnClass innerObj = new InnClass();

 innerObj.innerMethod();

 } 

 

 public static void main(String[] args)

 {

 OutClass outerObj = new OutClass();

 outerObj.outerMethod();

 }

}

Output:

Inside method

Inside method 2

  • Static Inner class

A static inner class performs the function of a static member of an outer class. Because it is a static member, we may use a static method to access it without having to initialize the outer class. So, essentially, Static Inner Classes are not Java Inner Classes. A static nested class cannot utilize the outer class’s instance variables and methods.

Code to understand Static Inner Class:

package com.tech.innerclass;

public class OutClassDemoSession

{

 static class NestDemoSession

 {

 public void myMethod()

 {

 System.out.println(“A static nested class”);

 }

 public static void main(String args[])

 {

 //Utilizing the static nested class without initializing the object.

 OutClassDemoSession.NestDemo nested = new 

 OutClassDemoSession.NestDemo();

 nested.myMethod();

 }

 }

}

Output:

A static nested class

  • Anonymous Inner class

An anonymous inner class can be declared without a name. It allows you to write more succinct code. In general, they are used when it is necessary to override a class or interface method.

We can use it when we just need to use a local class once. They are identical to local inner classes but don’t have a name.

Code to understand Static Inner Class:

package com.tech.innerclass;

interface Anonymousfruit

{

 void type();

}

public class AnonymousInClass

{

 public static void main(String args[])

 {

 AnonymousAnimal animal = new AnonymousAnimal(){

 public void type()

 {

 System.out.println(“APPLE”);

 System.out.println(“BERRY”);

 System.out.println(“CHERRY”);

 System.out.println(“KIWI”);

 }

 };

 fruit.type();

 }

}

Output:

APPLE

BERRY

CHERRY

KIWI

Conclusion

Inner classes are classes that act differently from their subclasses. We’ve learned about java inner classes and the necessity of utilizing them. We also discussed types of inner class in Java with examples. Enroll in UNext Jigsaw’s Postgraduate Certificate Program in Full Stack Development to learn, understand, and master the nuances of Java classes. This robust program with 4 Mini Projects, 1 capstone project & 10 real-world case studies for hands-on experience will help you become a competent java programmer in just 9 months.

Related Articles

} }
Request Callback