Cover Image for Java Inner classes
190 views

Java Inner classes

The inner class in Java is a class that is defined within another class. Inner classes provide a way to logically group classes that are closely related and encapsulate them within the outer class. Inner classes have access to the members (including private members) of the outer class, which can facilitate better encapsulation and organization of code.

There are four types of inner classes in Java:

  1. Member Inner Class:
    A member inner class is defined within the body of an outer class. It can access both static and non-static members of the outer class. To instantiate a member inner class, you generally need an instance of the outer class.
  2. Static Nested Class:
    A static nested class is similar to a member inner class, but it is declared as static. This means it can be accessed without creating an instance of the outer class. It does not have access to the non-static members of the outer class unless they are also marked as static.
  3. Local Inner Class:
    A local inner class is defined within a method or a code block. It has access to the variables and parameters of the enclosing method. Local inner classes are not accessible outside the method.
  4. Anonymous Inner Class:
    An anonymous inner class is a special type of local inner class that does not have a name. It’s often used to implement an interface or extend a class in a concise manner. Anonymous inner classes are commonly used in event handling and callbacks.

Here’s an example of each type of inner class:

public class OuterClass {
    private int outerField = 10;

    // Member Inner Class
    class MemberInner {
        void printOuterField() {
            System.out.println("Outer Field: " + outerField);
        }
    }

    // Static Nested Class
    static class StaticNested {
        void printMessage() {
            System.out.println("Static Nested Class");
        }
    }

    // Method with Local Inner Class
    void printLocal() {
        class LocalInner {
            void display() {
                System.out.println("Local Inner Class");
            }
        }
        LocalInner local = new LocalInner();
        local.display();
    }

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();

        // Member Inner Class instantiation
        MemberInner memberInner = outer.new MemberInner();
        memberInner.printOuterField();

        // Static Nested Class instantiation
        StaticNested staticNested = new StaticNested();
        staticNested.printMessage();

        // Local Inner Class within method
        outer.printLocal();

        // Anonymous Inner Class (implementing Runnable)
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Anonymous Inner Class (Runnable)");
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }
}

Inner classes provide a way to achieve tighter encapsulation and organization of code. However, they should be used judiciously and only when their logical relationship with the outer class is clear.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS