The difference between static, final, and static final in Java

Source: Internet
Author: User

Final

Final can be modified: properties, methods, classes, local variables (variables in methods)

The initialization of a final decorated property can be either at compile time or at run time, and cannot be changed after initialization.

The final decorated properties are related to the specific object, and the final property initialized at run time can have different values for different objects.

The final decorated property indicates a constant (cannot be modified after creation).

The final decorated method indicates that the method cannot be overridden in a subclass, and the final decorated class indicates that the class cannot be inherited.

For basic type data, final will change the value to a constant (cannot be modified after creation), but for an object handle (also known as a reference or pointer), final will change the handle to a constant (when declaring, the handle must be initialized to a specific object.) And you can no longer point the handle to another object. However, the object itself can be modified. This restriction also applies to arrays, arrays are also objects, and the array itself can be modified. The final handle in the method parameter means that within the method, we cannot change the actual thing that the parameter handle points to, that is, you cannot assign another value to the formal parameter handle inside the method.

Static

Static can be modified: properties, methods, code snippets, inner classes (static inner classes or nested inner classes)

Initialization of static modified properties can be changed at compile time (class loading), after initialization.

The static decorated property has only one value for all objects.

The properties of the static modifier emphasize that they have only one.

The static decorated properties, methods, and code snippets are independent of the specific object of the class, and the properties, methods, etc. of the static adornment can be called without creating the object

Static and "This, super" are at the same time, static is irrelevant to the specific object, and this, super is related to the specific object.

Static can not modify local variables.

Static final and final static:

Static final and final static are no different, general static written in front.

Static final:

The properties of the static modifier emphasize that they have only one, and the final decorated property indicates a constant (cannot be modified after creation). The static final decorated property indicates that once a value is given, it cannot be modified and can be accessed through the class name.

Static final can also modify the method, indicating that the method cannot be overridden and can be invoked without a new object

The difference between static, final, and static final in Java

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.