Inner class details - static nested class

definition

Static nested classes, also known as nested classes or static inner classes. It is an inner class decorated with the static keyword .

written form

General writing form:

public class Outter {
    /** Nested classes, public and other four access modifiers can be modified */
    public static class Inner {
        /** Inside the nested class */
    }
}

The writing form in the interface:

public interface OutInterface {
    /** Nested classes, nested classes defined in interfaces must be public, so the public modifier can be omitted */
    class Inner {
        public void print() {
            System.out.println("print()");
        }
    }
}

Syntax rules for nested classes

Rule 1: Non-static members of the outer class cannot be accessed from the nested class.


Rule 2: Unlike ordinary inner classes, the instantiation of nested classes does not need to depend on the outer class object, and has no necessary connection with the outer class.

public class Outter {
    /** Nested classes */
    public static class Inner {
        
    }
    public static void main(String[] args) {
        Outter.Inner inner = new Inner();
    }
}

Rule 3: The difference from ordinary inner classes is as follows:

Ordinary inner classes can only contain non-static variables , non-static methods , and non-nested classes .

Static nested classes can contain static and non-static variables , static and non-static methods , static and normal inner classes .


( Note : " contained in a nested class" and "access in a nested class" in Rule 1 have different meanings, the difference is that " access " is a member defined outside the nested class that calls the nested class, while "contains access in a nested class" " means defined in a nested class)

Rule 4: Nested classes can be defined in an interface and used as part of an interface.

"Java Programming Thought" (Fourth Edition, P203)

If you want to create some common code so that it can be shared by all the different implementations of an interface, it is convenient to use nested classes inside an interface.

Code example:

/** interface*/
public interface OutInterface {
    void method_1();
    void method_2();
    /** Nested classes */
    class Inner {
        public void print() {
            System.out.println("print something...");
        }
    }
}
/** Implementation class */
public class OutInterfaceImpl implements OutInterface {
    private OutInterface.Inner inner = new Inner();

    @Override
    public void method_1() {
        inner.print();
    }

    @Override
    public void method_2() {
        inner.print();
    }

    public static void main(String[] args) {
        OutInterface oif = new OutInterfaceImpl();
        oif.method_1();
        oif.method_2();
    }
}

Output result:

print something...
print something...

To sum up, it is the detailed use of static nested classes. The issues that need attention have been listed in the rules. After the bloggers practice, they are probably the same. If you have any questions or suggestions, please leave a message at the end of the article. *v*


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325403509&siteId=291194637