Deep understanding of final keywords in Java

Source: Internet
Author: User

The final keyword in Java is important, and it can be applied to classes, methods, and variables. In this article I will show you what is the final keyword? What does it mean to declare variables, methods, and classes as final? What are the benefits of using final? Finally, there are instances that use the final keyword. Final is often used with static to declare constants, and you'll see how final improves application performance.

What does the final keyword mean?

Final is a reserved keyword in Java and can declare member variables, methods, classes, and local variables. Once you have the reference declaration as final, you will not be able to change the reference, the compiler will check the code, and if you attempt to initialize the variable again, the compiler will report a compilation error.

What is a final variable?

A final variable is always declared as final for a member variable or a local variable (in a method or a variable in a code block, called a local variable). The final variable is often used with the static keyword as a constant. The following is an example of a final variable:

public static final String loan= "LOAN"; Loan=new String ("LOAN");//invalid compilation error

The final variable is read-only.

What is the final method?

Final can also declare methods. The method is preceded by the final keyword, which means that the method cannot be overridden by a quilt class. If you think that the functionality of a method is complete enough, and you don't need to change it in a subclass, you can declare that this method is final. The final method is faster than the non-final method because it is statically bound at compile time and does not need to be dynamically bound at run time. Here is an example of the final method:

Class person{public  final String getName () {        return "personal loan"    }}class loan extends person{      @ Override public        final Stirng getName () {                return ' cheap personal loan ';//compilation Error:overridden method is fi NAL        }}
What is the final class?

A class that uses final to decorate is called the final class. The final class is generally functionally complete and cannot be inherited. There are many classes in Java that are final, such as String, Interger, and other wrapper classes. The following is an instance of the final class:

Final class Personalloan{}class Cheappersonalloan extends personalloan{}  //compilation error:cannot inherit from Final class

  

The benefits of the final keyword

The following summarizes some of the benefits of using the final keyword

    1. The final keyword improves performance. Both the JVM and the Java application cache the final variable.

    2. The final variable can be shared securely in a multithreaded environment without the need for additional synchronization overhead.

    3. With the final keyword, the JVM optimizes methods, variables, and classes.

Immutable classes

To create an immutable class, use the final keyword. An immutable class means that its objects cannot be changed once they have been created. String is the representative of the immutable class. Immutable classes have many benefits, such as their objects are read-only, can be shared securely in multithreaded environments, without additional synchronization overhead, and so on.

Related reading: Why the string is immutable and how to write an immutable class.

Important points of knowledge about final
    1. The final keyword can be used for member variables, local variables, methods, and classes.

    2. The final member variable must be initialized at the time of declaration or initialized in the constructor, or a compilation error will be reported.

    3. You cannot assign a value to the final variable again.

    4. A local variable must be assigned a value at the time of declaration.

    5. All variables in an anonymous class must be final variables.

    6. The final method cannot be overridden.

    7. The final class cannot be inherited.

    8. The final keyword differs from the finally keyword, which is used for exception handling.

    9. The final keyword is easy to confuse with the Finalize () method, which is the method defined in the object class, which is called by the JVM before garbage collection.

    10. All the variables declared in the interface are final.

    11. The final and abstract two keywords are anti-related, and the final class cannot be abstract.

    12. The final method is bound during the compile phase, called static binding.

    13. No final variables, called blank final variable, are initialized at the time of declaration, they must be initialized in the constructor or called this () initialization. If you do not, the compiler will say "final variable (variable name) needs to be initialized".

    14. Declaring classes, methods, and variables as final can improve performance, so that the JVM has an opportunity to estimate and then optimize.

    15. According to the Java Code convention, the final variable is constant, and the pass is often capitalized:

    1. For a collection object to be declared final refers to a reference that cannot be changed, but you can add, delete, or change the content to it. Such as:

Private final List Loans = new ArrayList (); List.add ("Home Loan");  Validlist.add ("personal loan"); Validloans = new Vector ();  Not valid

  

We already know what the final variable, final method, and final class are. Use final when necessary to write faster, better code.

Deep understanding of final keywords 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.