Java.Core.What are the features of using nested classes: static and internal?

Features of Using Nested Classes in Java: Static and Non-Static (Internal)

Nested classes in Java help organize code, improve encapsulation, and reduce unnecessary dependencies. Java provides two main types of nested classes:

  1. Static Nested Classes (declared with static)
  2. Non-Static Nested Classes (Inner Classes) (also called “internal classes”)

Let’s explore their features and differences.

1. Features of Static Nested Classes

A static nested class is a class declared inside another class using the static keyword. It does not need an instance of the outer class to be used.

Features of Static Nested Classes

Belongs to the outer class, but does not depend on an instance
Cannot access non-static (instance) members of the outer class directly
Can contain both static and non-static members
✔ Can be accessed using OuterClass.StaticNestedClass
✔ Helps in grouping utility/helper classes inside another class

Example: Static Nested Class

class Outer {
    static class StaticNested {
        void display() {
            System.out.println("Inside Static Nested Class");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer.StaticNested obj = new Outer.StaticNested(); // No need for Outer instance
        obj.display();
    }
}

When to Use?

  • When a class logically belongs inside another class but does not need access to instance variables.
  • When defining utility/helper classes inside another class.

2. Features of Non-Static Nested Classes (Inner Classes)

An inner class (non-static nested class) is associated with an instance of the outer class. It can access all members of the outer class, including private fields.

Features of Inner Classes

Requires an instance of the outer class to be instantiated
Can access all members (even private) of the outer class
✔ Cannot have static members (except constants)
✔ Helps in encapsulation by hiding implementation details
✔ Supports tight coupling with the outer class

Example: Inner Class

class Outer {
    private String message = "Hello from Outer!";

    class Inner {
        void display() {
            System.out.println(message); // Accessing outer class private member
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner(); // Requires an instance of Outer
        inner.display();
    }
}

When to Use?

  • When the inner class needs access to the outer class’s members.
  • When the inner class is logically part of the outer class and should not exist independently.

3. Static Nested Class vs. Inner Class (Comparison)

FeatureStatic Nested ClassInner Class (Non-Static)
Requires an instance of the outer class?❌ No✅ Yes
Can access outer class instance variables?❌ No✅ Yes
Can have static members?✅ Yes❌ No (except constants)
Memory Usage✅ Less (does not store a reference to the outer class)❌ More (stores a reference to the outer class)
Encapsulation✅ Groups related logic without needing an outer instance✅ Can access private members of the outer class
InstantiationOuter.StaticNested obj = new Outer.StaticNested();Outer.Inner obj = outer.new Inner();

4. Special Use Cases

Using a Static Nested Class for Utility Methods

class MathOperations {
    static class Calculator {
        static int add(int a, int b) {
            return a + b;
        }
    }
}

public class Main {
    public static void main(String[] args) {
        int result = MathOperations.Calculator.add(5, 3);
        System.out.println("Sum: " + result);
    }
}

Why? The Calculator class is logically related to MathOperations, but does not need access to instance members.

Using an Inner Class for Accessing Outer Class Members

class Car {
    private String engineType = "V8";

    class Engine {
        void start() {
            System.out.println("Starting " + engineType + " engine...");
        }
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        Car.Engine engine = car.new Engine();
        engine.start();
    }
}

Why? Engine depends on Car, and an instance of Car is required to create Engine.

5. Summary

FeatureStatic Nested ClassInner Class (Non-Static)
Belongs toThe outer class (without needing an instance)The outer class and needs an instance
Access to Outer Class Members❌ No (cannot access instance members)✅ Yes (can access private members)
InstantiationOuter.StaticNested obj = new Outer.StaticNested();Outer.Inner obj = outer.new Inner();
Use CaseUtility/helper classes that don’t depend on an outer instanceClasses that need access to outer class members
Encapsulation✅ Groups logic inside a class✅ Helps in encapsulating details
This entry was posted in Без рубрики. Bookmark the permalink.