Summary of static and final usage in Java
Final
Final variable(Before adding the keyword final to the defined variable, it indicates that the variable cannot be changed once it is initialized .)
Unchangeable means:
The value of the basic type cannot be changed. The object variable reference cannot be changed. initialization can be performed in two places (either of the two can be selected ):
Variable definition (that is, assign a value to the final variable when it is defined) in the constructor, when the function parameter is of the final type, you can read and use this parameter, however, the parameter value cannot be changed. if the internal class defined in the method needs to use a method parameter, this parameter must also be declared as final for use.
Final Method(If a class does not allow its subclass to overwrite a method, you can declare this method as a final method)
There are two reasons for using the final method:
Lock method to prevent any inheritance class from modifying its meaning and implementation.
Efficient. The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.
Final class (The final class cannot be inherited.)
Therefore, the member methods of the final class have no chance to be overwritten. By default, the final class is used for modification, if this class does not require subclass or class implementation details and cannot be changed, it is designed as a final class static
Overview
The static modified member variables and member methods are independent of any objects in the class.
It is affiliated to the class itself, and is shared by all instances of the class without relying on the specific instance of the class.
That is to say
Static indicates that it can be used without instantiation.As long as this class has been loaded, JVM can find it in the Method Area of the runtime data zone based on the class name. A static object can be accessed before any of its objects are created without referencing any objects. if there is private modification before the static variable, it indicates
You cannot directly reference other classes by class names.Static variables can be used in static code blocks of the class, or in member methods of the class (including static and non-static ).
Static variable
Class member variables can be classified according to whether they are static or not. There are two types:
Static variablesThere is a static modifier (also called
Class variable)
Instance variablesThere is no static modification before it
The differences between the two are as follows:
For
StaticVariable
Memory savingThere is only one copy in the memory, and the static variable memory is allocated during the JVM loading process.
ConvenientDirect access to available class names (of course, you can also access through objects, but this method is not recommended)
InstanceVariable
FlexibleThere can be multiple copies in the memory. Each time an instance is created, the memory will be allocated once.
Static MethodStatic Method
Called directly by Class Name,
Any instance can also callStatic Method
Only static member variables and member methods of the class can be accessed.The this and super keywords cannot be used in the. static method.
The static method cannot be abstract modified.Because it is independent of any instance and must have a method body in the class.
Static code block
When the JVM loads a class, these static code blocks are executed, and each code block is executed only once.Static code blocks are static statement blocks independent of class members. there can be multiple static code blocks, which can be placed anywhere. If there are multiple static code blocks in any method, JVM will execute them in sequence according to their appearance in the class. final static appears at the same time
Static final is used to modify member variables and member methods.Global constant
For VariablesIndicates that the value cannot be modified and can be accessed by class name.
For MethodsIndicates that it cannot be overwritten and can be accessed directly through the class name.