[JAVA] Internal class type: With or without static

First, there are four types of nested classes (classes defined inside the class):

--*** static nested class *** --*** Non-static nested class *** --*** Local inner class *** --** Anonymous class ***

About static nested classes and non-static nested classes

――Both have the following uses --When using an object only inside the enclosing class [^ 1] --If you want to hide the nested class implementation inside the parent class --To save top-level names

--The behavior changes slightly depending on whether static is added or not.

MyObject.java


public class OuterObject{
    String outer = "";

    static void method() {
        InnerStaticObject innerStaticObject = new InnerStaticObject();
        InnerObject innerObject = new InnerObject(); //Compile error
    }

    /**static nested class**/
    private static class InnerStaticObject {
        void method() {
            System.out.println(outer); //Compile error
            System.out.println(OuterObject.this.outer); //Compile error
        }
    }

    /**Non-static nested class**/
    private class InnerObject {
        void method() {
            System.out.println(outer); //Can be referenced
            System.out.println(OuterObject.this.outer); //Can be referenced
        }
    }
}

static nested class

--*** does not have a reference to the enclosing object *** --Object creation of inner class (static nested class) can be done *** in the class method of the enclosing class ***

Non-static nested class

--*** with (implicitly) *** a reference to the enclosing object --Object creation of inner class (non-static nested class) cannot be done *** in class method of enclosing class ***

It seems that it is basically better to use with static because non-static causes extra dependence.

Local inner class

Classes defined in methods, constructors, initialization blocks, if clauses, and other blocks

――It has the following uses --If you want to hide the class implementation inside a block

MyObject.java


class MyObject {
    private void method() {
        //Local inner class
        class LocalInnerClass implements MyInterface {
            @Override
            public void print() {
                System.out.println("Local Inner Class");
            }
        }
        LocalInnerClass localInnerClass = new LocalInnerClass ();
        localInnerClass.print();
    }
}

interface MyInterface {
    void print();
}

Anonymous class

A class without a class name

――It has the following uses --No constructor required --Only one object creation

MyObject.java


class MyObject {
    private void method() {
        //Anonymous class
        MyInterface myInterface = new MyInterface() {
            @Override
            public void print() {
                System.out.println("Anonymous Class");
            }
        };
        myInterface.print();
    }
}

interface MyInterface {
    void print();
}

[^ 1]: Enclosing class: A class outside the inner class

Recommended Posts

Internal class type: With or without static
Processing speed with and without static
Instance concept, class type variables, constructors, static members
[Rails] ActiveModel :: Serializers with or without explicit nesting
Java getClass () is incompatible with generics (or type variables)
Java class type field
Find the address class and address type from the IP address with Java