Differences in Java inner class and static inner classes

Static inner classes and inner classes

Examples

public class OuterClass {
    private int numPrivate = 1;
    public int numPublic = 2;
    public static int numPublicStatic = 3;
    private static int numPrivateStatic = 4;

    public void nonStaticPublicMethod(){
        System.out.println("using nonStaticPublicMethod");
    }

    private void nonStaticPrivateMethod(){
        System.out.println("using nonStaticPrivateMethod");
    }

    public static void staticPublicMethod(){
        System.out.println("using staticPublicMethod");
    }

    private static void staticPrivateMethod(){
        System.out.println("using staticPrivateMethod");
    }

    class InnerClass{

        //Inner class cannot have static declarations
        //static int numInnerClass = 4;
        //public static void test(){}

        int numNonStaticInnerClass = 5;

        public void print(){
            System.out.println("using InnerClass");
            System.out.println("access private field: "+numPrivate);
            System.out.println("access public field: "+numPublic);
            System.out.println("access public static field: "+numPublicStatic);
            System.out.println("access private static field: "+numPrivateStatic);
            System.out.println("access numNonStaticInnerClass: "+numNonStaticInnerClass);
            nonStaticPrivateMethod();
            nonStaticPublicMethod();
            staticPrivateMethod();
            staticPublicMethod();
        }
    }

    static class StaticNestedClass{

        static int numStaticNestedClass = 6;
        int numNonStaticNestedClass = 7;

        public void print(){
            System.out.println("using StaticNestedClass");
            System.out.println("access public static field: "+numPublicStatic);
            System.out.println("access private static field: "+numPrivateStatic);
            System.out.println("access numStaticNestedClass: "+numStaticNestedClass);
            System.out.println("access numNonStaticNestedClass: "+numNonStaticNestedClass);
            staticPrivateMethod();
            staticPublicMethod();
        }
    }

    public static void main(String[] args) {
        //内部类实例对象
        OuterClass outerClass = new OuterClass();
        OuterClass.InnerClass innerClass = outerClass.new InnerClass();
        innerClass.print();
        System.out.println("=====================");
        //静态内部类实例化对象
        OuterClass.StaticNestedClass nestedClass = new OuterClass.StaticNestedClass();
        nestedClass.print();
    }
}

result

using InnerClass
access private field: 1
access public field: 2
access public static field: 3
access private static field: 4
access numNonStaticInnerClass: 5
using nonStaticPrivateMethod
using nonStaticPublicMethod
using staticPrivateMethod
using staticPublicMethod
=====================
using StaticNestedClass
access public static field: 3
access private static field: 4
access numStaticNestedClass: 6
access numNonStaticNestedClass: 7
using staticPrivateMethod
using staticPublicMethod

Static inner classes use

Access static inner classes through an external class

OuterClass.StaticNestedClass

Create a static inner class object

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Use inner class

We must first instantiate the outer class to instantiate inner class

OuterClass outerClass = new OuterClass();
OuterClass.InnerClass innerClass = outerClass.new InnerClass();

Difference between the two

  1. Inner class, even private can access, whether static or non-static can be accessed

    • Access to all the member variables and methods of the enclosing class (outside of class)
    • Private private member variables and methods of the enclosing class (outside of class) can also be accessed in
    • Not have static variables and static methods inner classes
  2. Static inner classes

    • The closure does not have access class (class external) non-static variable or a non-static method
    • Private static static private member variables and methods of the enclosing class (outside of class) can also be accessed in
    • You can have static variables and static methods static inner classes
  3. Internal class can be declared as private, public, protected, or package private. But the closed class (outside of class) can only be declared publicor package private

Special case

public class ShadowTest {

    public int x = 0;

    class FirstLevel {

        public int x = 1;

        void methodInFirstLevel(int x) {
            System.out.println("x = " + x);
            System.out.println("this.x = " + this.x);
            System.out.println("ShadowTest.this.x = " + ShadowTest.this.x);
        }
    }

    public static void main(String... args) {
        ShadowTest st = new ShadowTest();
        ShadowTest.FirstLevel fl = st.new FirstLevel();
        fl.methodInFirstLevel(23);
    }
}

Output

x = 23
this.x = 1
ShadowTest.this.x = 0

in conclusion

  1. ShadowTest class defines three names, like variable x

    • ShadowTest member variable x

    • FirstLevel inner class member variables x

    • methodInFirstLevel method parameter x

  2. methodInFirstLevel method parameter x in the shadow of the inner class FirstLevel, the use in the process of x when the methodInFirstLevel, x is pointing method parameter x, the result of x at this time is 23

  3. At this point inside this class FirstLevel scope, so the result is a this.x

  4. ShadowTest.this ShadowTest scope is pointed, then the result is 1 ShadowTest.this.x

Why inner classes

  • This is a method used only for the class in a logical grouping place : if a class is useful only to another class, the class and then embed the two are held together is logical.
  • It adds the package : Consider two top-level classes A and B, where B needs access to members of A, if a member is declared A privatethen B can not access. By hiding class B class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
  • This could lead to code more readable and maintainable : nested subcategories make the code outside the class closer to the position of use.

Serialization

It is strongly recommended not to inner classes (including local and anonymous classes) are serialized.

If the sequence of an inner class, then use another JRE achieve its deserialized, you may experience compatibility problems.

Serialization of inner classes, including local and anonymous classes, is strongly discouraged. When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs; these are classes, methods, fields, and other constructs that do not have a corresponding construct in the source code. Synthetic constructs enable Java compilers to implement new Java language features without changes to the JVM. However, synthetic constructs can vary among different Java compiler implementations, which means that .class files can vary among different implementations as well. Consequently, you may have compatibility issues if you serialize an inner class and then deserialize it with a different JRE implementation. See the section Implicit and Synthetic Parameters in the section Obtaining Names of Method Parameters for more information about the synthetic constructs generated when an inner class is compiled.

Guess you like

Origin www.cnblogs.com/eternityz/p/12577225.html