Introduction to Nested Class in Java
Nested class refers to a class that is inside of another class. Java allows us to create nested classes in java. A nested class is one of the members of its outer class. It can be declared as public, private, protected or default as well. The nested class has access to the other member of the outer class, while vice versa is not possible. It means the outer class doesn’t have access to a nested class member as the nested class is a member of its enclosing outer class, so dot (.) used to access the nested class & its members. The nested class doesn’t have any static keywords.
Categories of Nested Class in Java
A nested class is divided into two categories:
- Static Nested Class: Nested class declared with the keyword static is known as static nested class. Static Nested classes are accessible by referencing the outer class. The nested class which has a static class cannot access the nonstatic variables & methods of the outer class.
- Inner Class: Inner class, which is not statically declared known as a non-static nested class. No static nested classes would have access to all the static & non-static variables & methods even if it declared private. The inner class also has two types.
- Local inner classes
- Anonymous inner classes
Syntax:
In the below-given syntax, OuterClass having an inside class InnerClass that is known as Nested class.
//Outer class
class OuterClass {
//Inner class as a nested class
class InnerClass {
....
}
}
Use of Nested Class in Java
In the programming world, the Nested class plays an important role, as given below:
- Nesting of the class is like a grouping of classes in another class scope. Nested classes can not be used in other places.
- Leads to Readability & Maintainability: Nesting of classes provide better code readability, and it is also easier to maintain as it doesn’t affect the other classes.
- Reduced Code: The use of nested class reduces lines of code which leads to optimization.
- Increases Encapsulation: In nested classes, inner classes have access to the member of its enclosing outer class. while inner class members can not be accessed directly by the outer class. Also, the inner class is hidden from outside other programs.
Examples of Nested Class in Java
Given below are the examples of Nested Class in Java:
Example #1
In this example, we can see how inner classes are instantiated by referencing the outer class.
Code:
// Outer class which contains nested class
class Engine{
static double fuel = 20.0;
//static nested class
static class Ignition{
void showFuelSpend() {
System.out.println("Fuel Spend = " + fuel + " Ltrs");
}
}
}
//class uses nested class inside it
public class NestedClassExample{
public static void main(String[] args) {
//creating object of the nested class by referencing parent class
Engine.Ignition engIgnitObj = new Engine.Ignition();
//calling method of the nested class
engIgnitObj.showFuelSpend();
}
}
Output:
Example #2
In this example, we can see how the inner class can be instantiated. To create an instance of the inner class first Outer class instance will be required. Once the Outer class instance is created, then a nested class instance can be created inside it.
Code:
//Outer class
class Volume{
double x, y, z;
Volume(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
// Nested inner class
class Measurement{
//method to calculate the total area
void totalArea(double i, double j) {
System.out.println("\nArea for the provided container is " + (i * j));
}
//method to calculate the total volume
void totalVolume(double i, double j, double k) {
System.out.println("\nVolume for the provided container is " + (i * j * k));
}
}
}
public class NestedClassCalcExample {
public static void main(String[] args) {
//passing here all the parameter to constructor
Volume volObj = new Volume(30.0, 25, 18.0);
Volume.Measurement volMeasureObj = volObj.new Measurement();
// calculating total area
volMeasureObj.totalArea(volObj.x, volObj.y);
// calculating total volume
volMeasureObj.totalVolume(volObj.x, volObj.y, volObj.z);
}
}
Output:
Example #3
In this example, we can see how the nested class object is instantiated inside of the Outer class instance.
Code:
//outer class
class Electronic{
String circuitName,
String circuitType;
double circuitCost;
//constructor of outer class
Electronic(String name, String type, double cost) {
this.circuitName = name;
this.circuitType = type;
this.circuitCost = cost;
}
String getCircuitName() {
return this.circuitName;
}
//nested class
class Circuit{
String circuitType;
double circuitCost;
void setCircuitType() {
this.circuitType = "Transistor";
this.circuitCost = 430.0;
}
//get circuit type using this method
String getCircuitType(){
return this.circuitType;
}
//get circuit cost using this method
double getCircuitCost(){
return this.circuitCost;
}
}
}
public class Product{
public static void main(String[] args) {
Electronic elObj = new Electronic("Amplifier", "Integrated", 375.0);
Electronic.Circuit circuit = elObj.new Circuit();
//printing here the values before reset it
System.out.println("\nCircuit Name : " + elObj.circuitName);
System.out.println("\nCircuit Type : " + elObj.circuitType);
System.out.println("\nCircuit Cost : " + elObj.circuitCost);
//resetting some value
circuit.setCircuitType();
//printing here the values before reset it
System.out.println("\n\nCircuit Name : " + elObj.getCircuitName());
System.out.println("\nCircuit Type : " + circuit.getCircuitType());
System.out.println("\nCircuit Cost : " + circuit.getCircuitCost());
}
}
Output:
Conclusion
In the above-given article, we reviewed how the Nested class is important in java. It is good practice to utilize the nesting of classes. The usability of the Nested class is also described in the above article.
Recommended Articles
This is a guide to Nested Class in Java. Here we discuss the basic concept, Use of Nested Class in Java, along with examples in detail. You may also have a look at the following articles to learn more –
41 Online Courses | 29 Hands-on Projects | 305+ Hours | Verifiable Certificate of Completion
4.8
View Course
Related Courses