[java] static and final records

The difference between static, final, and static final in Java (transfer)
Explanation: Not necessarily accurate, but the fastest to understand.

final:

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

The initialization of final modified properties can be done at compile time or at run time, and cannot be changed after initialization.

The properties modified by final are related to specific objects. The final properties initialized at runtime can have different values ​​for different objects.

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

A final modified method means that the method cannot be overridden in a subclass, and a final modified class means that the class cannot be inherited.

For basic types of data, final will change the value into a constant (cannot be modified after creation); but for object handles (also known as references or pointers), final will change the handle to a constant (when declaring, the handle must be initialized to a specific object. And the handle can no longer be pointed to another object. However, the object itself can be modified. This restriction also applies to arrays, which also belong to objects, and the array itself can also be modified. The final handle in the method parameter means that we cannot change what the parameter handle points to inside the method The actual thing, that is to say, the formal parameter handle cannot be assigned another value inside the method).

static:

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

Static modified properties are initialized at compile time (when the class is loaded), and can be changed after initialization.

All objects with static modified properties have only one value.

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

Static modified properties, methods, and code segments have nothing to do with the specific objects of this class, and static modified properties, methods, etc. can be called without creating an object

Static and "this, super" are irreconcilable. Static has nothing to do with specific objects, while this and super are just related to specific objects.

static cannot modify local variables.

static final和final static:

There is no difference between static final and final static, and generally static is written in front.

static final:

The static modified attribute emphasizes that they have only one, and the final modified attribute indicates that it is a constant (cannot be modified after creation). The attribute modified by static final means that once the 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 rewritten and can be called without a new object

Guess you like

Origin blog.csdn.net/qq_25218219/article/details/122150957