background

When writing Swager’s mode for a new project, we used a nested Model, so we had static inner classes in our code. I talked to you a little bit at codeReview. In particular, Static modifiers are not the same as modifiers and variables.

define

1. The inner classes

  • You can put the definition of one class inside the definition of another class. This is called an inner class.
  • A nested class is a member of its enclosing class. Non-statically nested classes (inner classes) can access other members of a closed class, even if they are declared private. A statically nested class does not have access to other members of a closed class. OuterClass as an integrated component, a nested class can be declared private, public, protected, or package-specific. (Recall that you can only declare an external class public or wrap it as private.)

Static inner classes

  • Inside a normal class, add the static keyword to the inner class.
  • Like class methods and variables, statically nested classes are associated with their external classes. Like static class methods, a statically nested class cannot directly reference instance variables or methods defined in its enclosing class: it can only use them through object references. (Just like between two ordinary different classes)

3. Differences between the two

  • An inner class is a member variable of an outer class. In layman’s terms, you can only get the outer class by calling it first (but this is different from composition).
  • But a static inner class is just like a normal class. You can call it directly, go directly to the new object.

4. What does Java do with nested classes?

  • This is a way of logically grouping classes that are only used in one place: if a class is only useful to another class, it makes logical sense to embed it in that class and keep the two together. Nesting such helper classes makes their packages simpler.
  • It adds encapsulation: Consider two top-level classes A and B, where B needs to access A’s members or be declared private. By hiding class B in class A, you can declare A’s members private, and B can access them. Plus, B itself can be hidden from the outside world.
  • This can lead to more readable and maintainable code: nesting small classes in a top-level class brings the code closer to use.

Demo 

  • Talk is cheap, Show me Code;

Package static inner class; import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion; import javassist.runtime.Inner; import lombok.Data; /** * @authoryuanxindong * @date: 2020/6/19 10:25pm */ @data public class OuterClassDemo {String outerStr; public OuterClassDemo(String outerStr) { this.outerStr = outerStr; } @Data public static class StaticInnerClass{ public StaticInnerClass(String innerStr) { this.innerStr = innerStr; } private String innerStr; } @Data public class InnerClass{ private String innerClassStr; Public InnerClass(String outerStr) {this.innerclassStr = getOuterStr(); } } public static void main(String[] args) { OuterClassDemo staticClassDemo =new OuterClassDemo("Outer"); staticClassDemo.getOuterStr(); OuterClass OuterClass = new OuterClass(); // OuterClass OuterClass = new OuterClass(); StaticInnerClass innerClass = new StaticInnerClass("StaticInner");
        System.out.println(innerClass.getInnerStr());
        initInnerClass();
    }
    public static void initInnerClass(){
        OuterClassDemo staticClassDemo1 = new OuterClassDemo("OuterClassDemo");
        InnerClass innerClass = staticClassDemo1.new InnerClass("outerStr"); System.out.println(innerClass.getInnerClassStr()); }}Copy the code

Matters needing attention

  • Serialization it is strongly recommended not to serialize internal classes (both local and anonymous). When the Java compiler compiles certain constructs, such as inner classes, it creates synthetic constructs.
  • These are classes, methods, fields, and other constructs that have no corresponding construction in the source code. Synthetic constructs enable the Java compiler to implement new Java language features without changing the JVM. However, composite constructs can vary between Java compiler implementations, which means.class files can vary between implementations. Therefore, if you serialize an inner class and then deserialize it using another JRE implementation, you may encounter compatibility issues.

Core discussion

  • Now that we know about static inner classes, why does static modify a class and make it globally available to this class?
  • How can a Static method be referenced directly by a class? Classes that are static can also be used directly by other classes, independent of the object.

A: Let’s be clear

  • Static modifies a method of a class, indicating that the method has no relation to the class object, only to the class
  • A class member variable is a static variable. A class member variable is a static variable. The other type is a variable that is not modified static, called an instance variable. The difference between the two is:
  • For static variables, there is only one copy in memory (to save memory), and the JVM allocates memory for static variables only once, during class loading. Static variables can be accessed directly (conveniently) by the class name, or by objects (but this is not recommended).
  • Okay, here’s the deal. Static modifiers can be used globally. Modifiers can be used independently of the object, class, or other objects. They can also be used directly by other classes or initialized by themselves.

conclusion

  • Nested classes: inner classes and static inner classes
  • An inner class is a member of an external class and must rely on external objects to implement it. A static inner class, independent of dependencies, can be implemented by itself.
  • Static modifies variables, classes, methods, and code blocks. Universal universality. In the case of variables, which is completely unique, there will be multiple drinks, but there will not be multiple stores.