Keywords in essay ②java---the final keyword

Source: Internet
Author: User

One: Final keyword

final variable : A final variable that is declared final for a member variable or a local variable (a variable in a method or a block of code is called a local variable). The final variable is often used with the static keyword as a constant . The final variable is read-only.

Final method : The method is preceded by the final keyword, which means that the method cannot be overridden by a method of the 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.

Final class : A class that is decorated with final 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 benefits of the final keyword :

    • The final keyword improves performance. Both the JVM and the Java application cache the final variable.
    • The final variable can be shared securely in a multithreaded environment without the need for additional synchronization overhead.
    • With the final keyword, the JVM optimizes methods, variables, and classes.

Immutable Classes : Create immutable classes to 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.

⑥ points to note about the final keyword:

  • The final keyword can be used for member variables, local variables, methods, and classes.
  • The final member variable must be initialized at the time of declaration or initialized in the constructor, or a compilation error will be reported .
  • Cannot assign a value to the final variable again. A reference to a member variable of a reference type cannot be changed, but the content can be changed .
  • Local variables must be assigned at declaration time .
  • all variables in an anonymous class must be a final variable .
  • All variables in an anonymous class must be final variables.
  • 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 the method defined in the object class, which is called by the JVM before garbage collection.
  • All the variables declared in the interface are final.
  • The final and abstract keywords are reversed and cannot be used at the same time .
  • final methods are bound at compile time, called static bindings .
  • final variables that are not initialized at the time of declaration are called Blank final variables (blank final variable), which must be initialized in the constructor or called this () initialization. If you do not, the compiler will error "final variable (variable name) needs to be initialized".
  • 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.

Keywords in essay ②java---the final keyword

Related Article

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.