Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Instance Inner Class

An instance inner class (also called a non-static nested class) is a class defined within another class, known as the outer class. Instance inner classes are not declared as static, and they have access to the members (including private members) of the outer class. In this tutorial, we'll discuss the basics of instance inner classes in Java, including their use cases and how to create and access their objects.

  • Creating an Instance Inner Class

To create an instance inner class, simply define the class inside the outer class without using the static keyword.

Example:

OuterClass.java:

public class OuterClass {
    private String message = "Hello, World!";

    class InnerClass {
        public void displayMessage() {
            System.out.println("Message from outer class: " + message);
        }
    }
}

In this example, we create an outer class called OuterClass with a private member message. We then define an instance inner class called InnerClass within the outer class. The InnerClass has access to the message member of the outer class, even though it is private.

  • Creating and Accessing an Instance Inner Class Object

To create and access an instance inner class object, you need to first create an object of the outer class, then use that object to create an object of the inner class.

Main.java:

public class Main {
    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        inner.displayMessage(); // Message from outer class: Hello, World!
    }
}

In this example, we first create an object of the OuterClass called outer. We then use the outer object to create an object of the InnerClass called inner. Finally, we call the displayMessage() method on the inner object, which displays the message from the outer class.

  • Use Cases for Instance Inner Classes

Instance inner classes are useful in the following scenarios:

  • When you want to group related classes together for better organization and readability.
  • When you want to create a class that is only meaningful or useful in the context of the outer class.
  • When you want to access private members of the outer class without using getter and setter methods.

In conclusion, instance inner classes in Java are a powerful way to create classes that are closely related to their outer class, either logically or because they need access to the outer class's members. By understanding the basics of instance inner classes and knowing how to create and access their objects, you can leverage this feature to write cleaner and more organized code.

  1. Creating and using instance inner classes in Java

    // Outer class
    class Outer {
        private int outerVar = 10;
    
        // Instance Inner Class
        class Inner {
            void display() {
                System.out.println("Outer variable: " + outerVar);
            }
        }
    }
    
    // Usage
    Outer outer = new Outer();
    Outer.Inner inner = outer.new Inner();
    inner.display();
    
  2. Accessing outer class members from instance inner class

    class Outer {
        private int outerVar = 10;
    
        class Inner {
            void displayOuterVar() {
                System.out.println("Outer variable: " + outerVar);
            }
        }
    }
    
  3. Inheritance and polymorphism with instance inner classes

    class Outer {
        class Inner {
            void display() {
                System.out.println("Display from Inner class");
            }
        }
    }
    
    // Inheritance and polymorphism
    class Subclass extends Outer.Inner {
        @Override
        void display() {
            System.out.println("Display from Subclass");
        }
    }
    
  4. Visibility and encapsulation in Java instance inner classes

    class Outer {
        private int outerVar = 10;
    
        // Instance Inner Class
        class Inner {
            private int innerVar = 20;
    
            void display() {
                System.out.println("Outer variable: " + outerVar);
                System.out.println("Inner variable: " + innerVar);
            }
        }
    }
    
  5. Local variables and instance inner classes in Java

    class Outer {
        void outerMethod() {
            int localVar = 30;
    
            // Instance Inner Class accessing local variable
            class Inner {
                void displayLocalVar() {
                    System.out.println("Local variable: " + localVar);
                }
            }
    
            Inner inner = new Inner();
            inner.displayLocalVar();
        }
    }