Source: Haizi talked about the final keyword, presumably a lot of people are not unfamiliar, in the use of anonymous internal classes may often use the final keyword. In addition, the string class in Java is a final class, so today we'll look at the use of the final keyword. The following is the directory outline of this article: I. Basic usage of the final keyword two. In-depth understanding of the final keyword if there is an incorrect point, please be more understanding and welcome to correct. I. Basic usage of the final keyword in Java, the final keyword can be used to decorate classes, methods, and variables (including member variables and local variables). Here's a look at the basic usage of the final keyword in three ways. 1. Modifier class when a class is decorated with final, it indicates that the class cannot be inherited. In other words, if a class you never let him inherit, you can use final to decorate. Member variables in the final class can be set to final as needed, but note that all member methods in the final class are implicitly specified as the final method. When using the final modifier class, be careful to choose, unless the class is not intended to be inherited later or for security reasons, try not to design the class as the final class. 2. Modification method The following paragraph is from the Java programming thought, Fourth edition, page 143th: "There are two reasons for using the final method." The first reason is to lock the method in case any inherited class modifies its meaning, and the second reason is efficiency. In earlier versions of Java implementations, the final method was converted to inline invocation. However, if the method is too large, you may not see any performance gains from inline calls. In the recent Java release, these optimizations do not need to be made using the final method. "Therefore, the method is set to final only if you want to explicitly prohibit the method from being overwritten in the subclass. Note: The private method of the class is implicitly specified as the final method. 3. Modified variable modifier is the most used place in final use, and it is also the focus of this article. First look at the basic syntax of the final variable: for a final variable, if it is a variable of the base data type, its value cannot be changed once initialized, and if it is a variable of a reference type, it cannot be pointed to another object after it has been initialized. For example: In the previous section of the code, the re-assignment of the variable I and obj is an error. Two. In-depth understanding of the final keyword after understanding the basic usage of the final keyword, let's look at where the final keyword is easy to confuse. 1. What is the difference between a class's final variable and a normal variable? When you use final action on a member variable of a class, the member variable (note is the member variable of the class, the local variable only needs to be guaranteed to be initialized before use is assigned) must be defined in theis initialized in the constructor, and once the final variable is initialized, it cannot be assigned again. So what is the difference between a final variable and a normal variable? Here's an example: public class Test {public static void main (string[] args) {String a = "Hello2"; final String b = "Hello"; String d = "Hello"; String C = b + 2; String E = d + 2; System.out.println ((A = = c)); System.out.println ((A = = e)); }}12truefalse We can first think about the output of this problem. Why the first comparison result is true, and the second compares the result to Fasle. This is the difference between the final variable and the normal variable, when the final variable is the base data type and the string type, if you can know its exact value during compilation, the compiler will use it as a compile-time constant. That is, where the final variable is used, it is equivalent to the constant that is accessed directly, and does not need to be determined at run time. This is a bit like a macro substitution in C language. So in the preceding section of code, because the variable B is final decorated, it is treated as a compiler constant, so where B is used, the variable b is replaced directly with its value. The access to variable D needs to be linked at run time. Presumably the difference should be understood, but note that only when the final variable value is exactly known during compilation, the compiler does this optimization, such as the following code is not optimized: public class Test {public static void Main (string[] args) {string a = "Hello2"; final String B = Gethello (); String C = b + 2; System.out.println ((A = = c)); } public static String Gethello () {return "Hello";}} The output of this code is false. 2. Is the content of the object pointed to by the final modified reference variable variable? In the above mentioned the final modified reference variable once the initialization of the assignment can no longer point to other objects, then the reference variable point to the contents of the object is variable? Take a look at the following example: public class Test {public static void main (string[] args) {final MyClass MyClass = new MyClass (); System.out.println (++MYCLASS.I); }} class MyClass {public int i = 0;} This code can be successfully compiled and has output results, the output is 1. This means that after the reference variable is final decorated, it cannot point to another object, but the contents of the object it points to are mutable. 3.final and static often confuse static with the final keyword, and static acts on member variables to indicate that only one copy is saved, and final is used to ensure that the variable is immutable. See this example: public class Test {public static void main (string[] args) {MyClass myClass1 = new MyClass (); MyClass myClass2 = new MyClass (); System.out.println (MYCLASS1.I); System.out.println (MYCLASS1.J); System.out.println (MYCLASS2.I); System.out.println (MYCLASS2.J); }} class MyClass {public final double i = math.random (); public static Double J = Math.random ();} Running this code will reveal that the two J values are the same for each print, while the value of I is different. From here you can see the difference between final and static variables. 4. Why is an external local variable used in an anonymous inner class only a final variable? This question is explained in the previous Article blog post, "Inside the Java class," which is not described here. 5. Questions about final parameters The phrase "When you do not need to change an object variable in a method as a parameter, explicitly using final declaration, will prevent you from unintentionally modifying a variable that affects the calling method", I personally understand that it is inappropriate. Because either the argument is a variable of the base data type or a variable of a reference type, using the final declaration does not achieve the effect described above. It's clear from this example that the above code seems to make it impossible to change the value of the variable I in the method after the final modification.。 As everyone knows, the method ChangeValue and the main method of the variable i is not a variable, because the Java parameter is passed by the value of the pass, for the basic type of variable, the equivalent of a direct copy of the variable. So even without final modification, changing the value of the variable I within the method does not affect the I outside of the method. Look at the following code: public class Test {public static void main (string[] args) {MyClass MyClass = new MyClass (); StringBuffer buffer = new StringBuffer ("Hello"); Myclass.changevalue (buffer); System.out.println (Buffer.tostring ()); }} class MyClass {void ChangeValue (final stringbuffer buffer) {buffer.append ("World");}} Running this code will reveal that the output is HelloWorld. Obviously, finishing with final does not prevent changing the contents of the object that the buffer points to in ChangeValue. Some say that if the final is removed, what if buffer is pointed to other objects in the ChangeValue. Friends with this idea can write their own code to try what the result is, if the final is removed, and then in the ChangeValue to point buffer to other objects, and does not affect the buffer in the main method, because Java is based on value passing, For reference variables, the reference value is passed, which means that the argument and the parameter point to the same object simultaneously, so that having the parameter point back to the other object has no effect on the argument. Therefore, I personally do not agree with the statement on the final parameters circulated on the Internet.
Found a good article, said very detailed, first collection.