The final keyword represents the immutable. The following is a discussion of the three scenarios used by final keywords: data, methods, and classes.
Final data 1, final property
Some "constants" often need to be used in a program. Constants are mainly used in two ways:
- Compile-time constants, never change
- Initializes a value at run time and does not want it to change.
For compile-time constants, calculations can be performed ahead of time during compilation, and the constant values can be used directly in the program. In Java, this constant must be a basic data type. The predecessor keyword Final declaration. A value must be provided when defining .
class Person { final String name; // name not initialized, compilation error }
If you use Final,final with an object handle, the handle becomes a constant. when declaring , you must initialize the handle to a specific object , and you cannot point the handle to another object.
class Person { = "Zhang San";} Public class Finaldemo { publicstaticvoid main (string[] args) { Finalnew person (); New Person (); // Error: Unable to assign value to final variable p }}
However, the object itself can be modified.
class Person { = "Zhang San";} Public class Finaldemo { publicstaticvoid main (string[] args) { Finalnew person (); = "Rustling the cold"; }}
One possible result:
name = FD1,I1 = 0, i2 = 6
name = fd2,i1 = 8, i2 = 6
I1,i2 are randomly generated data during operation.
2. Blank Final
Java1.1 allows you to create "blank final", which belongs to a special field. Although it is declared final, it does not have an initial value. Even so, the blank final must be initialized before it is used. Example:
classPerson {} Public classFinaldemo {Final inti; FinalPerson p; Finaldemo () {i= 1; P=NewPerson (); } Finaldemo (intx) {i=x; P=NewPerson (); } Public Static voidMain (string[] args) {Finaldemo fd=NewFinaldemo (); }}
It is now mandatory to assign the final assignment, either by using an expression when defining the field, or in each builder.
3, with final modification parameters
looked up some information, and many people said that the final modification method parameter was to prevent the parameter from being modified at the time of invocation. Personally, there are two kinds of interpretations: one is that the actual value of the variable is not modified, and the other is not modified within the method. The previous statement is wrong whether it is a basic parameter type or a reference type. Because .
public class Finaldemo { void F (final int i) {i + +; // cannot assign a value to the final variable, compile error void main (string[] args) { int x = 10; f (x); // ①
The F method called at ① only assigns the value of x to I, in fact I and X are two variables.
Let's look at the following example:
class Person { = "Zhang San";} Public class Finaldemo { publicstaticvoid main (string[] args) { Finalnew person (); ChangeName (p); System.out.println (p.name); } Static void changename (final person p) { = "rustling the Cold";} }
"Run Results":
The rustle of the cold
As a result, final does not prevent the ChangeName () method from changing the contents of P. Next, we delete the final of the decorated parameter and then change the instance of P pointing in the ChangeName method body:
classPerson {String name= "Zhang San";} Public classFinaldemo { Public Static voidMain (string[] args) {FinalPerson p =NewPerson (); P.name= "Rustle of the cold"; ChangeName (P); System.out.println (P.name); } Static voidchangename (person p) {p=NewPerson (); }}
"Run Results":
Name in ChangeName: Zhang San
The rustle of the cold
We can see that although p in the method body points to other objects, it has no effect on p in the main method. The reason is that Java is a value pass. Specifically, refer to Java value delivery or reference delivery?
Final method
The final method has two main functions: one is to prevent any inherited class coverage methods . This can be done if you want the behavior of a method to remain unchanged during inheritance, not overwritten and rewritten. The other is to improve the efficiency of program execution . When a method is set to final, the compiler ignores the general code insertion method that is taken to execute the method invocation mechanism (the argument is pressed into the stack; jumps to the method code and executes it; jumps back; clears the stack argument, and finally processes the return value). It replaces the method call with a copy of the actual code within the method body. This avoids the overhead of the method invocation. If the method body is too large, the efficiency may not be improved.
class Human { publicfinalvoid Show () { //... }}publicclassextends human{ public void Show () {} //cannot override the final method from Human}
all private methods in the class automatically become final. Because a private method cannot be accessed, it is never overwritten.
Final class
If the entire class is final, it indicates that the class is not allowed to be inherited. Or for security reasons, you don't want to subclass. In addition, it might also consider the issue of efficiency and ensure that all actions involving such objects are as effective as possible.
Final class Human {} Public class extends // The type man cannot subclass the final class Human}
Note: data members can be either final or not. The rule applied to final applies to the data member, regardless of whether the class is defined as final.
after the class is defined as final, the result is only forbidden from being inherited. because inheritance is forbidden, all methods in a final class default to final.
The final keyword in Java