I. Summary of final and static in JAVA |
|
|
Final |
Static |
Modifier |
This class cannot be inherited |
Only internal classes can be modified. This class does not require new and is statically loaded (nested top-level classes) |
Modifier Interface |
× |
× |
Modifier Constructor |
× |
× |
Modify statement Block |
× |
The Virtual Machine initializes the static member field and the static statement block in the declared order. |
Modify Field attributes |
Attribute cannot be modified after initialization (instance constant) |
Is a class variable. All instances share the variable value (class variable) |
Assign values in definitions or Constructors (each instance has a copy) |
Modifier |
This method cannot be overwritten (only inherited) |
Yesclass method. All instances share this method (class method) |
Modify function parameters |
Parameter values cannot be modified by functions |
× |
Modifier local variable |
Local variables cannot be modified after initialization (local constants) |
× |
|
|
|
Purpose |
1). To prevent methods from being overwritten or rewritten; |
1) define global variables or constants; |
2). Improve the running efficiency. JAVA uses an embedded mechanism for final method calls; |
2). Define class methods; |
|
|
|
Ii. JAVA class loading sequence |
JAVAClass loading sequence: |
1) recursively Load Static members/code blocks in the order of code, first parent class and then this class; |
|
2) recursively load non-static members/code blocks in the order of code, first parent class and then this class; |
|
3) The constructor is called recursively in the order of code, and the parent class is added to the class; |
|
|
JAVAClass addition time: |
1) when a static member is called, the class and parent class of the static member are loaded; |
|
2). Load the new object for the first time (the new object will not be loaded for the second time) |
|
3). The parent class will be loaded before the subclass is loaded. Members with static and final modifiers will not be loaded and used as constants; |