Iii. final
Final is not commonly used in Java, but it provides functions such as defining constants in C language, final also allows you to control whether your members, methods, or a class can be overwritten or inherited. These features enable final to play an indispensable role in Java, it is also one of the keywords that must be known and mastered when learning Java.
Final member
When you define a variable in a class and add the final keyword before it, this variable cannot be changed once initialized, the unchangeable meaning here is that its value is immutable for the basic type, and its reference for the object variable cannot be changed. It can be initialized in two places. One is its definition, that is, it is assigned a value directly when the final variable is defined, and the other is in the constructor. You can only choose one of these two places, either give a value at the time of definition, or give a value in the constructor, not both at the time of definition, in the constructor, another value is given. The following code demonstrates this point:
- ImportJava. util.List;
- ImportJava. util.ArrayList;
- ImportJava. util.Shortlist;
- Public ClassBat {
- FinalPI = 3.14;// The address value is assigned at the time of definition.
- Final IntI;// Because Initialization is required in the constructor, no value can be given here
- Final ListList;// This variable is the same as the above variable.
- Bat (){
- I = 100;
- List =New Shortlist();
- }
- Bat (IntIi,ListL ){
- I = ii;
- List = l;
- }
- Public Static VoidMain (String[] Args ){
- Bat B =NewBat ();
- B. list. add (NewBat ());
- // B. I = 25;
- // B. list = new ArrayList ();
- System. Out. println ("I =" + B. I + "List Type:" + B. list. getClass ());
- B =NewBat (23,New ArrayList());
- B. list. add (NewBat ());
- System. Out. println ("I =" + B. I + "List Type:" + B. list. getClass ());
- }
- }
This program demonstrates the general usage of final. Here we use the initialization method in the constructor, which gives you a little flexibility. As shown in the two overloaded constructors of Bat, the first default constructor will provide you with the default value. The overloaded constructor will initialize the final variable based on the value or type you provided. However, sometimes you don't need this flexibility. You just need to set the value at the time of definition and never change. In this case, you don't need to use this method. There are two lines of statements commented out in the main method. If you remove the comment, the program will not be able to compile. This is to say, whether it is the value of I or the type of list, once initialized, it cannot be changed. However, B can specify the I value or list type through reinitialization. This is shown in the output:
I = 100 List Type: class java. util. Category List
I = 23 List Type: class java. util. ArrayList
Another method is to define the final parameter in the method. For variables of the basic type, this method has no practical significance, because the variables of the basic type are passed values when calling the method, that is to say, you can change this parameter variable in the method without affecting the call statement. However, it is very practical for the object variable because the object variable is passed with its reference during transmission, in this way, your modification to the object variable in the method will also affect the object variable in the call statement. When you do not need to change the object variable used as the parameter in the method, use final to declare it, it will prevent you from accidentally modifying the call method.
In addition, when the internal class in the method uses the variable in the method, this parameter must be declared as final for use, as shown in the following code:
- Public ClassINClass {
- VoidInnerClass (Final StringStr ){
- ClassIClass {
- IClass (){
- System. Out. println (str );
- }
- }
- IClass ic =NewIClass ();
- }
- Public Static VoidMain (String[] Args ){
- INClass inc =NewINClass ();
- Inc. innerClass ("Hello ");
- }
- }
Final Method
If you declare the method as final, it means that you already know that the function provided by this method has met your requirements and does not need to be extended, this method cannot be overwritten by any class inherited from this class, but inheritance can still inherit this method, that is, it can be used directly. In addition, there is a mechanism called inline, which enables you to directly Insert the method subject into the call when calling the final method, rather than performing routine method calls, such as saving breakpoints, this may improve the program efficiency. However, when your method subject is very large, or you call this method in multiple places, then your calling subject code will expand rapidly.