Java final modifier

Source: Internet
Author: User

  Final definition:

At the English level, final means "final", "final" meaning, which in Java also means the meaning.

  Final objects of application:

Final applies to modified variables (including class properties, object properties, local variables, and formal parameters), methods (including class methods and Object methods), and classes.

Final Decorated class:

Because final objects cannot be modified or overloaded, the final decorated class cannot be inherited, that is, it cannot have its own subclass. If the view inherits from a class that has already been final decorated, during compilation or an error occurs.

Final Modified variables:

  Once the final variable is defined and initialized for the first time, the value of the final modified variable cannot be changed.

Here are a few things to keep in mind:

1. Final modified variables, whether they are class properties, object properties, formal parameters, or local variables, require display initialization (that is, specifying an initial value for their display).

For final decorated formal parameters, it is well understood that the arguments are passed over.

For a final decorated local variable, it is necessary to display initialization, just like a variable with a final decoration. That is, local variables are required to display initialization.

For general class and object properties, as can be seen during initialization of classes and objects, the default initialization is performed first. The variable that displays the assignment is then initialized again.

However, for the class and object properties of the final decoration, if initialization is not displayed, it defaults to the default initialized value, which contradicts the starting point of the final itself, so the Java syntax provides:

The final decorated class properties and variable properties must be displayed for initialization assignment.

In addition, the final modified variables are the first display of initialization values that cannot be modified, whether for the base data type or the reference data type. For basic data types, it's good to understand. For reference

Data type, the reference variable points to the actual object, but it stores the address of the object being pointed to, so its value cannot be modified and does not imply that the object it points to cannot be modified.

Final assignment:

There are no real constants in Java. Only the final type of variable can be used as represented.
A final variable, only two places can be assigned:
1. You can assign values at the time of definition.
2. You can assign a value in the constructor. If you assign a value at definition, you cannot assign a value in the constructor. Includes a null value when defined, it is no longer assignable in the constructor.

Cases:

     Public Final String AAAA;      Public Doggie () {        AAAA= "Good Morning";    }
 Public Final String aaaa= "AAAA";

Give a special example again as the difference treats:

 Public class Test {    publicstaticvoid  main (string[] args) {        = ' China ';        = "Qqyumidi";         = country + name;  // country and name variables are still here after compilation        String user = "China" + "Qqyumidi";  // after compiling, it becomes Chinaqqyumidi directly .     }}

Another way to do this is:

 Public class Test {    publicstaticvoid  main (string[] args) {        final String country = "China";         Final String name = "Qqyumidi";         = country + name;  // This is Chianqqyumidi directly after compilation .     }}

Java final modifier

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.