java keyword (iv)--final

Source: Internet
Author: User

For the final keyword in Java, we can first understand the literal meaning, Baidu translation shows as follows:

  

In other words, the final English meaning is final and cannot be changed. The corresponding meaning is also expressed in Java, you can use the final keyword to modify variables, methods, and classes. Whatever it is intended to mean is "it can't be changed", which is something we need to keep in mind, why can't we change it? Just as the design required or can improve efficiency, with the previous introduction of the Static keyword need to remember its object-independent concept, remember the final immutable design concept and then to understand the final keyword usage, it will be natural.

1. Modifier variables

A little bit of Java basics know that variables decorated with the final keyword are called constants, and constants mean they cannot be changed. Variables are basic data types, and cannot be changed is easy to understand, so what about reference types? Is it possible to change the reference address or the content of the object?

We first construct an entity class: person

1  PackageCom.ys.bean;2 3 /**4 * Create by Ysocean5  */6  Public classPerson {7     PrivateString name;8 9      PublicPerson (String name) {Ten          This. Name =name; One     } A  -      PublicString GetName () { -         returnname; the     } -  -      Public voidsetName (String name) { -          This. Name =name; +     } -}
View Code

Then create a person object based on:

  

As you can see, you first decorate an object p with the final keyword, then point the P object to another new object and find an error, meaning that the reference type of the final decoration cannot change its reference address.

We then change the Name property of the P object:

  

The Discovery program did not error.

Conclusion: A final modified variable cannot change its reference address, but it can change its internal properties.

2. Modification method

The final keyword-decorated method cannot be overridden.

This is described in the final method p176 page of the 4th edition of the Java programming idea 7.8.2: There are two reasons for using the final method:

①, the first reason is to lock the method to prevent any inherited class from modifying its meaning, which is a design consideration: you want to make sure that the behavior of the method remains the same in inheritance and is not overwritten.

②, the second reason is efficiency, in the early implementation of Java, if a method is declared final, it is agreed that the compiler will make all calls to the method inline, the inline call can improve the efficiency of the method call, but if the method is large, inline calls will not improve performance. In the current Java version (after JDK1.5), the virtual machine can be automatically optimized without the need to use the final method.

So the final keyword only uses its adornment method when the override method is explicitly forbidden.

PS: "Java programming thought" indicates that all private methods in a class are implicitly specified as final, so for private methods, we explicitly declare that final has no effect. But why do we create a parent class and declare a private method in the parent class whose subclass is the private method that can override its parent class?

Parent class: Parent.class

 Package Com.ys.bean; /**  */Publicclass  Parent    {privatevoid  Say ( {        System.out.println ("parent");}    }

Sub-Category: Son.class

 Package Com.ys.bean; /**  */Publicclassextends  Parent    {private  void  Say () {        System.out.println ("son");}    }

Actually look closely, this writing is the method of coverage? We cannot call the Say () method of the parent class by polymorphic form:

  

Also, if we add @Override annotations in the subclass's say () method, it will be an error.

  

So this form does not count as a method of coverage.

3. Decoration class

The final decorated class indicates that the class cannot be inherited.

This means that you do not want a class to have subclasses, which are decorated with the final keyword. And because it is a final-modified class, all the methods in its class are implicitly referred to as the final method.

One of the most obvious class string in the JDK, which is the final decoration, is important for the final modification of the String class because it is a constant pool. For a description of the String class, refer to my blog post.

  

java keyword (iv)--final

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.