Java keywords final and static

Source: Internet
Author: User

Final

 

Final Chinese translations mean "final". Java can be used to modify classes, methods, variables, and method parameters.

 

Class

The final modified class cannot be inherited, so the Member methods in the final class cannot be overwritten. By default, they are all final. When designing a class, if the class does not need a subclass, the implementation details of the class cannot be changed, and it is sure that the class will not be loaded and extended, it is designed as a final class.

 

Method
If a method in a class does not allow its subclass to overwrite, you can declare this method as a final method.The compiler transfers the final method to the embedded mechanism to greatly improve the execution efficiency.

 

Final variable (constant) 
The final modified member variable is used to represent constants, and the value cannot be changed once given.

 

Final Parameters 
When the function parameter is of the final type, you can read and use this parameter, but you cannot change the value of this parameter.

 

 

 

Static 

Static indicates the meaning of "global" or "static". It is used to modify member variables and member methods.

 

The static modified member variables and member methods are independent of any object in the class. That is to say, it is shared by all instances of the class and does not depend on a specific instance of the class.

As long as the class is loaded, the Java Virtual Machine can locate the class names in the Method Area of the runtime data zone. Therefore, a static object can be accessed before any of its objects are created without referencing any objects.

The implication is that you can use

"Class Name. Method Name(Parameter list ...)"Or" class name. variable name.

 

 

Static and final


Static final is used to modify member variables and member methods. It can be simply understood as "Global constants" and "global methods".
For a variable, it means that once the value is given, it cannot be modified and can be accessed through the class name.
For methods, it means they cannot be overwritten and can be accessed directly by class names.


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.