The use of final in Java

Source: Internet
Author: User

JAVAMiddle FinalUsage of

1. finishing the final of the underlying data member

this is Final The main purpose of the application, meaning the equivalent C + + of the Const , that is, the member is decorated as a constant, meaning that it cannot be modified. such as PI and E in the Java.lang.Math class are final Members whose values are 3.141592653589793 and 2.718281828459045.

2. final of the reference to the decorated class or object

in theJava, we cannot let the object be decorated asFinalAnd can only modify the object's reference, which means that even if you writePublic Final A = new A ();in factaThe data of the pointed object can still be modified and cannot be modifiedathe reference value of itself, that is, you can no longerato re-assign the value. The same situation appears in the array, such asPublic Final int[] A = {1, 2, 3, 4, 5}, in factathe values in are modifiable, that is, you can writeA[0] = 3. It is now understood thatJavaThe data inside the array cannot be modified to be non-modifiable, andC + +you can.

3. Final of the modification method

of the Modification method Final and the C + + in the decorated member object. Const very different. First, the final meaning of the adornment method is not " non-modifiable ", but the method cannot be redefined by the inherited member. (Note that what is said here cannot be redefined, not that the subclass must not define a method of the same name, if the parent class's method is a private type, the subclass is allowed to define the method, and here refers to the inability to redefine the method to make the method rewrite the polymorphism can be implemented, such as do not want to A = new B (); A.F (); such an overriding method occurs)

Example:

public class A {

//Final Method F

Public final void F () {

System.out.println (" final method F in class A is called ");

}

}

public class Bextends A {

   // Compile Error! The F method of the parent class is the final type and cannot be overridden!

//! public void F () {

   //! System.out.println (" method F in class B is called ");

//! }

}

Also, when a method is decorated with final method means that the compiler might use the method inline with the mode of loading, so-called inline means that the compiler does not have to call the function as usual way to invoke the method, but directly the code inside the method through a certain modification after the copy ( " int[] arr = new Int[3] call arr.length ()

private methods, on the other hand, are implicitly decorated by the compiler as Final , which means private final void F () and the private void f () There is no difference.

4. Final of the modified class

when a class is decorated as Final , It is very clear that the class is not allowed to be inherited, that is, the class " unrepeatable " , any operation that inherits it will end up with a compilation error. This also underscores the reason that Java uses final instead of const as an identifier. ( the member variable may not be final, the member method is final )

Example:

Public final class A {

}

// Compile Error! a is the final type and cannot be inherited!

!public ClassB extends a{

//!}

5. final parameter

make final modifications to object parameters . The object variable is passed its reference, and is decorated to prevent unintentional changes during invocation.

The use of final 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.