In the Java world, the concept of static is often referred to as static member variables and modifier of member functions, meaning that it is shared for all instances of the class, that is, when an instance of a class modifies the static member variable, its modified value is seen by all other instances of the class. A recent project has frequently used static modified internal classes, and then read "effective Java" to see why it uses static to decorate an inner class that is the center of this article-static class.
if a class is to be declared static, there is only one case, the static inner class. If the external class is declared static, the program will not compile. After a survey, the individual summed up 3 points about inner class and static inner Class (commonly known as: inline Class)
1. Static internal classes, like static methods, can access only static member variables and methods, cannot access non-static methods and properties, but ordinary inner classes can access member variables and methods of any external class
2. Static inner classes can declare ordinary member variables and methods, while ordinary inner classes cannot declare static member variables and methods.
3. Static inner classes can be initialized individually:
New Outer.Inner ();
Normal internal class initialization:
New Outer (); Inner i = O.new Inner ();
A static inner class usage scenario is typically when an external class needs to use an inner class, and the inner class does not need an external class resource, and the inner class can be created separately , taking into account the design of the static inner class, knowing how to initialize the static inner class in the effective Java The static internal class builder described in Chapter II describes how to use static inner classes:
PublicClassOuter {PrivateString name;PrivateIntAgePublicStaticClassBuilder {PrivateString name;PrivateIntAgePublic Builder (IntAge) {This.age =Age }PublicBuilder withname (String name) {THIS.name =NameReturnThis; }Public Builder Withage (IntAge) {This.age =AgeReturnThis; }PublicOuter Build () {ReturnNew Outer (This); } }PrivateOuter (Builder b) { this.age = b.age; this.name = b.name;}}
The static inner class calls the constructor of the outer class to construct the outer class, since the static inner class can be initialized by itself and there is an external implementation of the following:
PublicOuter Getouter () {Outer Outer = new Outer.builder (2). Withname ("Yang Liu"). Build (); return outer;}
For the static class summary is: 1. If you have multiple parameters in a class's constructor or static factory, it is best to use the builder mode when designing such a class, especially if most of the parameters are optional.
2. If you cannot determine the number of parameters now, it is a good idea to use the builder as Builder mode at the beginning.
Transferred from: https://www.cnblogs.com/Alex--Yang/p/3386863.html
Java Static class