Examples of final keyword usage in Java _java

Source: Internet
Author: User
Tags constructor exception handling garbage collection

Final is often used with static to declare constants, and you will see how final improves application performance.
What does the final keyword mean?
Final is a reserved keyword in Java that can declare member variables, methods, classes, and local variables. Once you have the quote declared 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 final variable?
Any member variable or local variable (called a local variable in the method or in the code block) declared as final is called the final variable. Final variables are often used with the static keyword as constants. Here is an example of the final variable:

Copy Code code as follows:

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. method is preceded by the final keyword, which means that this method cannot be overridden by a quilt class. If you think that the function of a method is sufficiently complete and that the subclass does not need to be changed, you can declare that the method is final. The final method is faster than a non-final method because it is statically bound at compile time and does not need to be dynamically bound at run time. The following is an example of the final method:

Copy Code code as follows:

Class personalloan{
Public final String GetName () {
Return "personal loan";
}
}

Class Cheappersonalloan extends personalloan{
@Override
Public final String GetName () {
Return "cheap personal loan"; Compilation Error:overridden is final
}
}

What is the final class?
The class that is decorated with final is called the final class. Final classes are usually complete and cannot be inherited. Many of the classes in Java are final, such as String, Interger, and other wrapper classes. Here is an example of the final class:

Copy Code code as follows:

Final class personalloan{
}

Class Cheappersonalloan extends personalloan{//compilation error:cannot inherit from final class
}

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

The final keyword improves performance. Both the JVM and the Java application cache the final variable.
Final variables can be safely shared in a multithreaded environment without the need for additional synchronization overhead.
Using the final keyword, the JVM optimizes methods, variables, and classes.
Non-variable class
Create immutable classes to use the final keyword. A immutable class is one whose object cannot be changed once it is created. A string is a representation of immutable classes. Immutable classes have many advantages, such as their objects are read-only, can be securely shared in a multithreaded environment, without additional synchronization overhead, and so on.
Related reading: Why strings are immutable and how to write an immutable class.
Important points of knowledge about final
The final keyword can be used for member variables, local variables, methods, and classes.
The final member variable must be initialized at the time it is declared or initialized in the constructor, or a compilation error will be reported.
You are not able to assign a value to the final variable again.
Local variables must be assigned at the time of declaration.
All variables in an anonymous class must be final variables.
The final method cannot be overridden.
The final class cannot be inherited.
The final keyword differs from the finally keyword, which is used for exception handling.
The final keyword is easy to confuse with the Finalize () method, which is defined in the object class and is invoked by the JVM before garbage collection.
All the variables declared in an interface are final in themselves.
Final and abstract These two keywords are inverse related, the final class can not be abstract.
The final method is bound in the compile phase, called a static binding (binding).
The so-called blank final variable (blank final variable) that initializes the final variable at declaration time must be initialized in the constructor or called this () initialization. If you do not, the compiler will complain that the final variable (variable name) needs to be initialized.
Declaring classes, methods, and variables final can improve performance so that the JVM has an opportunity to estimate and then optimize.
In the Java Code convention, the final variable is a constant, and the pass-by-volume name is capitalized:
Private final int COUNT = 10;
For a collection object to be declared final, the reference cannot be changed, but you can add to it, delete it, or change the content. Such as:
Private final List loans = new ArrayList ();
List.add ("Home loan"); Valid
List.add ("personal loan"); Valid
Loans = new Vector (); Not valid
We already know what the final variable, final method, and final class are. Use final when necessary to write faster and better code.

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.