How should final function on variables, parameters, methods and classes in Java be handled
The function of modifying parameters with final in Java methods
Adding the final keyword in front of method parameters is to prevent data from being modified in the method.
There are two main situations: first, modify the basic data type with final; Second, use final to modify the reference data type.
In the first case, modify the basic data type. At this time, the value of the parameter cannot be modified in the method body, that is, it cannot be re assigned. Otherwise, the compilation will not pass.
In the second case, modify the reference type. At this time, the object referenced by the parameter variable cannot be changed. However, for reference data types, it is entirely possible to modify their properties.
Therefore, the keyword final is very useful if you want to use the basic data type.
Final variable:
For basic types, use final: it is a constant, and the value is constant
Use final for object reference: make the reference constant. Once the reference is initialized to point to one object, it can no longer point to another object. However, the object itself can be modified, and Java does not provide a way to make any object constant. This restriction also uses arrays, which are also objects.
example:
analysis:
For FD1 and fd2, I4 is unique, that is, each object has an I4, but int_ 5 is declared as static, that is, it is shared by classes, and FD1 and fd2 share int_ 5. It has been initialized during loading, rather than each time a new object is created (for example, i4); but it is also set to final, so its reference cannot be changed, that is, it cannot be modified to point to another object.
Blank final:
It is declared final, but no initial value is given. The final field must be assigned with an expression in the definition of the field or in each constructor, which is why the final field is always initialized before use.
Final parameter:
This means that you cannot change the parameter reference in the method to point to another parameter, but you can modify what the final object points to
example:
analysis:
The parameter is declared as final. If it is a basic parameter, it is a constant and cannot be modified; If it is a reference variable, it cannot be modified to point to another object, but the content of the object to which the reference refers can be modified.
Fianl method:
Reason for use:
All private methods in the class are implicitly specified as final. Since the private method cannot be accessed, it cannot be overwritten. You can add a final modifier to the private method, but this does not add any additional meaning to the method.
example:
analysis:
When overrides occur:
1. There are methods completely consistent with the parent class in the subclass
2. A subclass can be transformed upward into a parent class and call that method in the parent class
If as like as two peas in a parent class, final or private is declared, this method is invisible to the subclass, and even if it creates the same method as the parent class in the subclass, this is a new method instead of the method covered from the parent class.
Final class:
That is, this class cannot be inherited, whether you or others, that is, this class does not need to make any changes or any subclasses, such as string class.
example:
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.