The basic usage of final

Source: Internet
Author: User

1, the final basic usage
1.1. Cosmetic 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 be inherited, it can be decorated with final. The member variables in the final class can be final as needed, but note that all member methods in the final class are implicitly specified as final methods.

When using the final modifier class, be careful to choose, unless the class is really not intended to be inherited or for security reasons, try not to design the class as the final class.
1.2, modify the method
The following passage is excerpted from the fourth edition of Java Programming ideas, page 143th:
"There are two reasons to use the final method." The first reason is to lock the method in case any inheriting class modifies its meaning, and the second reason is efficiency. In an earlier version of the Java implementation, the final method was converted to an inline call. But if the method is too large, you may not see any performance boost from the inline call. In the most recent Java version, you do not need to use the final method for these optimizations. “
Therefore, the method is set to final only if you want to explicitly prohibit the method from being overwritten in a subclass.
Note: The private method of a class is implicitly specified as the final method.
1.3. Modifying variables
The modified variable is the most used place in final, which is the focus of this article. First, take a 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 it is initialized, and if it is a variable of a reference type, it cannot be directed to another object after it has been initialized.

In the preceding section of the code, the values for the variables I and obj are not correct.
2. Deep understanding
2.1. What is the difference between the final variable of the class and the ordinary variable?
When you apply final to a member variable of a class, a member variable (note is a member variable of a class, where a local variable is only guaranteed to be initialized before use) must be initialized in the definition or constructor, and the final variable can no longer be assigned once the assignment has been initialized.
So what's the difference between the final variable and the ordinary variable? Let's take a look at the following example:
[Java] View plain copy
1.

@Test
2. public void Test () {
3. String a = "Hello2";
4. Final String b = "Hello";
5. String d = "Hello";
6. String C = b + 2;
7. String E = d + 2;
8. System.out.println ("C:" +c + ", E:" + e);
9. System.out.println ("(A = = c):" + (A = = c) + ", (a = = e):" + (A = = e));
System.out.println ("(A.equals (c)):" + (A.equals (c)) + ", (A.equals (E)):" + (A.equals (e)));
11.}
Result: C:hello2, e:hello2 (a = = c): True, (a = = e): False (A.equals (c)): True, (A.equals (E)): true why (A = = c The first comparison is true, and the second compares to Fasle. This is the difference between the final variable and the ordinary variable, and when the final variable is the base data type and the string type, the compiler will use it as a compile-time constant if it knows its exact value during compilation. That is, where the final variable is used, the equivalent of direct access to this constant is not required to be determined at run time. This is a bit like a macro replacement in C language. So in the preceding code, because variable B is final decorated, it is treated as a compiler constant, so the variable B is replaced directly with its value where B is used. Access to variable D, however, needs to be done at run time through a link. Presumably the difference should be understood, but be aware that the compiler will do this only if you know exactly what the final variable value is during compilation, such as the following code will not be optimized:
[Java] View plain copy
1. public class Test {
2. public static void Main (string[] args) {
3. String a = "Hello2";
4. Final String B = Gethello ();
5. String C = b + 2;
6. System.out.println ((A = = c)); False
7.}
8.
9. public static String Gethello () {
return "Hello";
11.}
12.}
2.2. The content of the object pointed to by the final modified reference variable is variable.
[Java] View plain copy
1. public class Test {
2. public static void Main (string[] args) {
3. Final MyClass MyClass = new MyClass ();
4. SYSTEM.OUT.PRINTLN (++MYCLASS.I);
5.}
6.}
7.
8. Class MyClass {
9. public int i = 0;
10.}
This code can be compiled smoothly and has output results of 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.

2.3. Final and static
Many times it is easy to confuse the static with the final keyword, which is used to indicate that only one copy is saved, and the final function is to ensure that the variable is immutable. Look at the following example:
[Java] View plain copy
1. public class Test {
2. public static void Main (string[] args) {
3. MyClass MyClass1 = new MyClass ();
4. MyClass myClass2 = new MyClass ();
5. SYSTEM.OUT.PRINTLN (MYCLASS1.I);
6. SYSTEM.OUT.PRINTLN (MYCLASS1.J);
7. SYSTEM.OUT.PRINTLN (MYCLASS2.I);
8. SYSTEM.OUT.PRINTLN (MYCLASS2.J);
9.
10.}
11.}
12.
Class MyClass {
Public final Double i = math.random ();
public static Double J = Math.random ();
16.}
When you run this code, you will find that the two J values printed each time are the same, while the value of I is different. From here you can see the difference between final and static variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.