in , ,

Java Nested and Inner Class

Java Nested and Inner Class
Java Nested and Inner Class

Java Nested and Inner Class

Java Nested and Inner Class: In this tutorial, you will find out about the nested class in Java and its sorts with the help of examples.

In Java, you can characterize a class inside another class. Such a class is known as a nested class. For instance,

class OuterClass {
    // ...
    class NestedClass {
        // ...
    }
}

There are two types of nested classes you can create in Java.

Non-static nested class (inner class)
Static nested class

Recommended reading:

Java Access Modifiers
Java Static Keyword
Let’s first look at non-static nested classes.


Non-Static Nested Class (Inner Class)

A non-static nested class is a class inside another class. It approaches individuals from the encasing class (external class). It is normally known as the inner class.

Since the internal class exists inside the external class, you should start up the external class first, in order to instantiate the inner class.

Here’s an example of how you can declare inner classes in Java.

Example 1: Inner class

class CPU {
    double price;
    // nested class
    class Processor{

        // members of nested class
        double cores;
        String manufacturer;

        double getCache(){
            return 4.3;
        }
    }

    // nested protected class
    protected class RAM{

        // members of protected nested class
        double memory;
        String manufacturer;

        double getClockSpeed(){
            return 5.5;
        }
    }
}

public class Main {
    public static void main(String[] args) {

        // create object of Outer class CPU
        CPU cpu = new CPU();

       // create an object of inner class Processor using outer class
        CPU.Processor processor = cpu.new Processor();

        // create an object of inner class RAM using outer class CPU
        CPU.RAM ram = cpu.new RAM();
        System.out.println("Processor Cache = " + processor.getCache());
        System.out.println("Ram Clock speed = " + ram.getClockSpeed());
    }
}

Output

Processor Cache = 4.3
Ram Clock speed = 5.5

In the above program, there are two nested classes: Processor and RAM inside the outer class: CPU. We can declare the inner class as protected. Hence, we have declared the RAM class as protected.

Inside the Main class,

  • we first created an instance of an outer class CPU named cpu.
  • Using the instance of the outer class, we then created objects of inner classes:
CPU.Processor processor = cpu.new Processor;

CPU.RAM ram = cpu.new RAM();

Note: We use the dot (.) operator to make a case of the internal class using the external class.


Access Members of Outer Class within Inner Class

We can access to the members from the external class by using this watchword. If you want to learn about this keyword, visit Java this keyword.

Example 2: Accessing Members

class Car {
    String carName;
    String carType;

    // assign values using constructor
    public Car(String name, String type) {
        this.carName = name;
        this.carType = type;
    }

    // private method
    private String getCarName() {
        return this.carName;
    }

// inner class
    class Engine {
        String engineType;
        void setEngine() {

           // Accessing the carType property of Car
            if(Car.this.carType.equals("4WD")){

                // Invoking method getCarName() of Car
                if(Car.this.getCarName().equals("Crysler")) {
                    this.engineType = "Smaller";
                } else {
                    this.engineType = "Bigger";
                }

            }else{
                this.engineType = "Bigger";
            }
        }
        String getEngineType(){
            return this.engineType;
        }
    }
}

public class Main {
    public static void main(String[] args) {

// create an object of the outer class Car
        Car car1 = new Car("Mazda", "8WD");

        // create an object of inner class using the outer class
        Car.Engine engine = car1.new Engine();
        engine.setEngine();
        System.out.println("Engine Type for 8WD= " + engine.getEngineType());

        Car car2 = new Car("Crysler", "4WD");
        Car.Engine c2engine = car2.new Engine();
        c2engine.setEngine();
        System.out.println("Engine Type for 4WD = " + c2engine.getEngineType());
    }
}

Output

Engine Type for 8WD= Bigger
Engine Type for 4WD = Smaller

In the above program, we have the inner class named Engine inside the outer class Car. Here, notice the line,

if(Car.this.carType.equals("4WD")) {...}

We are using this keyword to access the carType variable of the outer class. You may have noticed that instead of using this.carType we have used Car.this.carType.

It is since, in such a case that we had not referenced the name of the external class Car, at that point, this keyword will speak to the part inside the inward class.

Likewise, we are additionally getting the method of the outer class from the inner class.

if (Car.this.getCarName().equals("Crysler") {...}

It is important to note that, although the getCarName() is a private method, we are able to access it from the inner class.


Static Nested Class

In Java, we can also define a static class inside another class. Such class is known as static nested class. Static nested classes are not called static inner classes.

Unlike inner class, a static nested class can’t access to the part variables of the external class. It is on the grounds that the static nested class doesn’t expect you to make an example of the external class.

OuterClass.NestedClass obj = new OuterClass.NestedClass();

Here, we are making an object of the static nested class by essentially using the class name of the external class. Consequently, the external class can’t be referred to using OuterClass.this.

Example 3: Static Inner Class

class MotherBoard {

   // static nested class
   static class USB{
       int usb2 = 2;
       int usb3 = 1;
       int getTotalPorts(){
           return usb2 + usb3;
       }
   }

}
public class Main {
   public static void main(String[] args) {

       // create an object of the static nested class
       // using the name of the outer class
       MotherBoard.USB usb = new MotherBoard.USB();
       System.out.println("Total Ports = " + usb.getTotalPorts());
   }
}

Output

Total Ports = 3

In the above program, we have created a static class named USB inside the class MotherBoard. Notice the line,

MotherBoard.USB usb = new MotherBoard.USB();

Here, we are making an object of USB using the name of the external class.

Now, let’s see what would happen if you try to access the members of the outer class:


Example 4: Accessing members of Outer class inside Static Inner Class

class MotherBoard {
   String model;
   public MotherBoard(String model) {
       this.model = model;
   }

   // static nested class
   static class USB{
       int usb2 = 2;
       int usb3 = 1;
       int getTotalPorts(){
           // accessing the variable model of the outer classs
           if(MotherBoard.this.model.equals("MSI")) {
               return 4;
           }
           else {
               return usb2 + usb3;
           }
       }
   }
}
public class Main {
   public static void main(String[] args) {

       // create an object of the static nested class
       MotherBoard.USB usb = new MotherBoard.USB();
       System.out.println("Total Ports = " + usb.getTotalPorts());
   }
}

When we try to run the program, we will get an error:

error: non-static variable this cannot be referenced from a static context

This is on the grounds that we are not using the object of the external class to make an object of the internal class. Subsequently, there is no reference to the external class Motherboard stored in Motherboard.this.


Key Points to Remember

  • Java treats the inner class as an ordinary member of a class. They are much the same as methods and variables pronounced inside a class.
  • Since internal classes are members of the external class, you can apply any access modifiers like private, protected to your inner class which is not possible in ordinary classes.
  • Since the settled class is a member of its encasing external class, you can use the dot (.) notation to access the nested class and its members.
  • Using the nested class will make your code more readable and give better encapsulation.
  • Non-static nested classes (inner classes) approach different members of the external/encasing class, even if they are declared private.

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Encapsulation

Java Encapsulation

Java Nested Static Class

Java Nested Static Class