Static and final knowledge point finishing

Source: Internet
Author: User
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.

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. The final variable is read-only.

method is preceded by the final keyword, which means that this method cannot be overridden by a quilt class. 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 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.

The benefits of the final keyword
1 Final keyword improves performance. Both the JVM and the Java application cache the final variable.
2 The final variable can be safely shared in a multithreaded environment without the need for additional synchronization overhead.
3 using the final keyword, the JVM optimizes the methods, variables, and classes.

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.

static denotes "global" or "static" meaning, used to decorate member variables and member methods, or to form static-code blocks, but the concept of global variables is not available in the Java language.

The member variables and member methods that are decorated by static are independent of any object of the class. That is, it does not rely on class-specific instances and is shared by all instances of the class. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the runtime data area. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.

A code block decorated with static represents a static code block, which is executed when the Java Virtual Machine (JVM) loads the class.

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.