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.
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.
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.
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:
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.
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.
Placing inner classes into top-level classes brings the code closer to where it will be utilized.
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
}
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.
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.
Four types of inner classes:
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.
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 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.
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();
Inside method
Inside method 2
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.
public class OutClassDemoSession
static class NestDemoSession
public void myMethod()
System.out.println(“A static nested class”);
//Utilizing the static nested class without initializing the object.
OutClassDemoSession.NestDemo nested = new
OutClassDemoSession.NestDemo();
nested.myMethod();
A static nested 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.
interface Anonymousfruit
void type();
public class AnonymousInClass
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();
APPLE
BERRY
CHERRY
KIWI
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.
Fill in the details to know more
How To Use the Pivot Table in Excel ?
May 12, 2023
Role of Cost in Pricing of the Product!
April 18, 2023
What Is Data Visualization in Excel?
April 14, 2023
What Are Databases and Tables in SQL?
March 24, 2023
It’s Raining Opportunities In Cloud Computing!
March 23, 2023
Product Management – With Great Power Comes Great Responsibility!
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile