Differences between static, final, and static final in java

Source: Internet
Author: User

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

The final modified attributes can be initialized during the compilation or runtime, but cannot be changed after initialization.

The final modifier attributes are related to specific objects. The final attributes initialized at runtime can have different values for different objects.

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

The final modifier method indicates that the method cannot be overwritten in the subclass, and the final modifier class indicates that the class cannot be inherited.

For basic data types, final changes the value to a constant (cannot be modified after creation), but for object handles (also called references or pointers ), final changes the handle to a constant (the handle must be initialized to a specific object during declaration. In addition, you cannot direct the handle to another object. However, the object itself can be modified. This restriction applies to arrays, arrays also belong to objects, and arrays can also be modified. The final handle in the method parameter means that inside the method, we cannot change the actual thing that the parameter handle points to, that is, the parameter handle cannot be assigned another value within the method ).

 

 

Static can be modified: attributes, methods, code segments, internal classes (static internal classes or nested internal classes)

Static modified attributes are initialized during the compilation period (when the class is loaded) and can be changed after initialization.

All objects of the static property have only one value.

Static modified attributes emphasize that there is only one of them.

The attributes, methods, and code segments of the static modifier have nothing to do with the specific objects of the class. If you do not create an object, you can also call static modifier attributes and methods.

Static and "this, super" are not independent. static has nothing to do with specific objects, while this and super are related to specific objects.

Static cannot modify local variables.

 

There is no difference between static final and final static. Generally, static is written before.

Static modifier attributes emphasize that there is only one of them. final modifier attributes indicate a constant (cannot be modified after creation ). The property modified by static final indicates that once a value is given, it cannot be modified and can be accessed by class name.

Static final can also be used to modify a method, indicating that the method cannot be rewritten and can be called without a new object.

 

 

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.