Inner class and static inner class in Java

Static inner class in Java: add the static modifier before the defined inner class. At this time, the inner class is the static inner class. Through an example to understand the characteristics of static inner classes.

public class Outer {
    //Define an instance variable and a static variable
    private int a;
    private static int b;
    //Define a static method and a non static method
    public static void say(){}
    public void test(){
        //Property and method of calling inner class in external class
        Outer.Inner.c = 1;            //Static properties of static internal classes can be called by the full class name of static internal classes (external class name. Static internal class name. Property)
        Outer.Inner.go();            //Static methods of static internal classes can be called by the full class name of static internal classes (external class name, static internal class name, method)
        //Outer.Inner.walk(); / / non static properties and methods of an inner class cannot be called by the full class name of the inner static class
        Inner inner = new Inner();
        inner.d = 1;
        inner.walk();                    //Non static properties and methods of static inner classes can be called by creating inner class instances
    }
    //Static inner class
    public static class Inner{
        //Define a static variable and an instance variable in a static inner class
        static int c;
        int d;
        //Define an anonymous code block and a static code block
        {}
        static{}
        //Define a static method and a normal method
        public static void go(){}
        public void walk(){
            //The properties and methods of calling external class in static internal class
            int f = b;                     //Static properties of external classes can be called directly
            say();                        //Static methods of external classes can be called directly
            //int e = a; error calling nonstatic attribute of external class directly compile error
            //test(); compilation error when calling non static method of external class directly
            Outer outer = new Outer();
            int e = outer.a;            //You can call the non static properties of an external class by creating an external class instance
            outer.test();                //You can call the non static methods of an external class by creating an external class instance
        }
    }
}

Summary: 1. What can be written in static internal classes

1) anonymous code block

2) static code block

3) static and non-static variables

4) static method and non static method

Note: abstract methods cannot be written in static inner classes

2. How external classes call properties and methods in static internal classes

1) external classes can call the non-static properties and methods of static internal classes by creating methods of static internal class instances

2) the external class can directly call the static attributes and methods in the static internal class by the way of "external class, internal class, attribute (method)"

3. How static internal classes call the properties and methods of external classes

1) static internal classes can directly call static properties and methods of external classes

2) static inner class can call the non static properties and methods of the outer class by creating the method of the outer class instance

4. How to create a static internal class instance

1) in non external classes: external class name. Internal class name = new external class name. Internal class name ();

2) in the external class: internal class name = new internal class name ();

Keywords: Programming Attribute Java

Added by danlayton00 on Mon, 01 Jun 2020 17:15:53 +0300