Usage of the final keyword in Java

Source: Internet
Author: User

Final:adj. The final, immutable

We take the literal meaning "immutable".

Final can modify classes, methods, and variables. So what is the difference?

(1) Modifier class: Indicates that the class cannot be inherited

(2) Modification method: Indicates that the method cannot be overwritten

(3) Modifier variable: Indicates that the variable can not change its value once it is assigned. In Java, a final decorated member variable must be displayed by the programmer to display the value of the specified variable.

Where do you want to execute the initial value when the final decorated member variable ?

(1) If the final decoration is a class variable, you can specify the initial value only in the static initialization block or when declaring the class variable.

(2) If the final decoration is a member variable, you can execute the initial value in a non-static initialization block, declaring the variable, or the constructor.

Note that the initial value can only be specified in one place!!

Final modifier local variable

The system does not initialize local variables, and local variables must be initialized by the programmer's display. Therefore, when you use final to modify a local variable, you can specify the default value when you define it (the subsequent code cannot assign a value to the variable), or you can specify no default value, and the final variable is assigned an initial number (only once) in the following code.

What is the difference between a final decorated base type variable and a reference type variable?

When final modifies a primitive type variable, the base type cannot be re-assigned.

However, for a reference variable, it simply holds a reference, and final guarantees that the address referenced by the variable of the reference type will not change. The same object is always referenced, but the value of this object can be changed.

Class person () {    private int age;              Public person () {} public Person    (int.)    {        this.age = age;    }}    public class finalreferencetest{    main ()    {        final int[] iarr={1,2,3,4};        Arrays.sort (Iarr);//Legal        iarr[2]=-3;//legal          iarr=null;//illegal, to Iarr can not be re-assigned        final person p = new person;        P.setage (24);//Legal        p=null;//illegal      }    }

  

Final Modification method

The final decorated method cannot be overridden. For example, the GetClass () method in the object class is the final method. cannot be overridden.

The private method in the parent class cannot access the method in the subclass, but the subclass can still define a method that has the same method name, the same formal parameter list, and the same return value as the parent private method, not a method override, but a new method is defined.

The final modification method is simply not overridden and cannot be overloaded. The following code, which is a method overload, still has no errors!

public class finalclass{public     final void Test () {} public     final void test (int i) {}}

  

Final Modifier class

Ah

Usage of the final keyword in Java

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.