Final
Final Variable (Before the keyword final is added to the defined variable, the variable is not changed once it is initialized.)
- The specific meaning of an immutable term is:
- Basic type whose value cannot be changed
- Object variable whose reference cannot be changed
- Its initialization can be done in two places (both can be selected):
- The definition of a variable (that is, assigning a value directly to the final variable definition)
- In the constructor
- When a function parameter is a final type, it can be read using the parameter, but the value of the parameter cannot be changed.
- The inner class defined inside the method must also be declared final to use if the parameter of the method is required.
Final Method (If a class does not allow its subclasses to overwrite a method, declare this method as the final method)
There are two reasons for using the final method:
- The locking method prevents any inheriting class from modifying its meaning and implementation.
- efficient . The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.
Final Class (final class cannot be inherited )
- Therefore, the member methods of the final class have no chance to be overwritten, and the default is final to decorate
- When designing a class, if the class does not need to have subclasses, the implementation details of the class are not allowed to change then it is designed as the final class
Static
-
Overview
member variables and member methods that are modified by static are independent of any object of that class.
It belongs to the class itself and does not depend on class-specific instances, shared by all instances of the class
- that is, static means that you do not need to instantiate to use
- as long as this class is loaded, the JVM can be found in the method area of the runtime data area based on the class name.
A
- static object can be accessed before any of its objects are created, without referencing any objects.
- a private adornment before a static variable means that cannot refer directly to a class name in another class
The
- static variable can be used in a static code block of a class, or in a member method of a class (including static and non-static)
- The
-
static variable
is divided into two categories according to whether the class member variable is static:
- Static code block
- These static blocks of code are executed when the JVM loads the class, and each code block is executed only once
- Also called a static block of code, which is the static statement block that is independent of the class member in the class.
- can have multiple, position can be placed casually, it is not in any method body
- If there are multiple static code blocks, the JVM executes them sequentially in the order in which they appear in the class.
Final static appears simultaneously
Static final is used to modify member variables and member methods, so that you can easily understand global constants
for variable representations, once a value is given, it cannot be modified and can be accessed through the class name.
The method representation is not overwritten and can be accessed directly through the class name.
Summary of static and final usage in Java