Sobes.tech
Middle

What types of nested classes exist?

sobes.tech AI

Answer from AI

Nested classes in Java are divided into two main types:

  1. Static Nested Classes

    • Marked with the static modifier.
    • Behave like regular static members of the container class.
    • Can access static members of the outer class (even if they are private).
    • Do not have access to non-static members of the outer class directly.
    • Are created without an instance of the outer class: OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();.
  2. Inner Classes

    Inner classes are not marked with the static modifier and are associated with an instance of the outer class. They are subdivided into:

    • Member Inner Classes

      • Declared at the member level of the container class, outside its methods.
      • Have access to all members of the outer class, including private ones.
      • Require an instance of the outer class to create their own instance: OuterClass outerObject = new OuterClass(); OuterClass.MemberInnerClass innerObject = outerObject.new MemberInnerClass();.
    • Local Classes

      • Created inside methods, constructors, or initialization blocks.
      • Scope is limited to the block in which they are declared.
      • Cannot be declared with access modifiers (public, private, protected).
      • Can access final or effectively final local variables of the outer block.
      • Cannot be used outside the block where they are defined.
    • Anonymous Classes

      • Essentially, these are nameless local classes.
      • Declared and instantiated simultaneously.
      • Used to implement an abstract class or interface "on the fly".
      • Like local classes, they have access to final or effectively final local variables of the outer block.

Example structure:

class OuterClass {
    private int outerField;
    private static int staticOuterField;

    // Static nested class
    static class StaticNestedClass {
        void accessOuter() {
            // Can access static members of the outer class
            int value = staticOuterField;
            // Cannot access non-static members of the outer class
            // int value2 = outerField; // Compilation error
        }
    }

    // Member inner class
    class MemberInnerClass {
        void accessOuter() {
            // Can access all members of the outer class
            int value1 = outerField;
            int value2 = staticOuterField;
        }
    }

    void methodWithLocalAndAnonymous() {
        final int localVar = 10; // effectively final

        // Local class
        class LocalClass {
            void accessOuter() {
                // Can access members of the outer class and local final/effectively final variables
                int value1 = outerField;
                int value2 = staticOuterField;
                int localValue = localVar;
            }
        }

        LocalClass localObj = new LocalClass();
        localObj.accessOuter();

        // Anonymous class (implementing Runnable interface)
        Runnable anonymousRunnable = new Runnable() {
            @Override
            public void run() {
                // Can access members of the outer class and local final/effectively final variables
                int value1 = outerField;
                int value2 = staticOuterField;
                int localValue = localVar;
                System.out.println("Running with local variable: " + localValue);
            }
        };

        anonymousRunnable.run();
    }
}