Detailed explanation of final Java keywords

Introduce concepts

  Final is a keyword in the Java language. Final means final and immutable, which means that the final modified part has the attribute of "unchangeable". The parts that can be modified by final are variables, methods, and classes. Let's discuss them one by one below.

final modified variable

  The final keyword can be used in variable declarations. Once the variable is set, the value of the variable can no longer be changed. Usually, the variables defined by final are constants.
Final modifies local variables Variables
  defined by the final keyword must be assigned values ​​when they are declared. Local variables modified by final cannot be modified by secondary assignment, they can only be assigned once.
Final modified instance variables
  . Ordinary instance variables without final modification. If we do not manually assign values ​​during declaration, the system will assign default values. The system does not assign default values ​​to final modified instance variables. This requires programmers to manually assign values. This manual assignment can be directly assigned after the variable is declared, or in the construction method of the class where the instance variable is located. Assignment among them.
  We know that variables in Java are divided into variables of basic data types. Of course, this variable can also be a reference to an object. What if the final modified variable is a "reference"? ? ?
Once an object reference is modified as final, it can only point to one object constantly, and cannot be changed to point to another object. Even if a reference modified as final is assigned a value of null, it is considered assignment once. At the same time, the null reference will not be directly reclaimed by the garbage collector, and the space will be released only after the method containing the null reference ends.
Tips: Final modified instance variables are generally modified with static.
  We know that an object reference defined as final can only point to only one object, and it cannot point to other objects, but the value of an object itself can be changed, so in order to make a constant truly unchangeable, you can Declare the constant as static final .

final modification method

  Methods defined as final cannot be overridden.Defining a method as a final type can prevent subclasses from modifying the definition and implementation of the class. At the same time, the execution efficiency of the method defined as final is higher than that of the non-final method. One point that needs to be raised here is that for the permission modifier private, if a method of a parent class is set to private modification, the subclass will not be able to access the method and naturally cannot override the method, so a method defined as private The method is implicitly designated as a final type, so there is no need to redefine a method defined as a private as a final type.

final modified class

  Classes defined as final cannot be inherited.If you want a class to not allow any class inheritance, and not allow others to make any changes to this class, you can set this class to final form. From this, we know that the abstract keyword and the final keyword are opposite and cannot coexist.
Tips: Abstract-modified classes are specifically used to be inherited, while final-modified classes are not allowed to be inherited.
  If a certain class is set to final form, all methods in the class are implicitly set to final form, but member variables in the final class can be defined as final or non-final.


After reading this blog post, the question we need to think about is:
What are the similarities and differences between final modified constants and static modified static variables? ? ?


Tips: The answer is as follows: the
same point: constants and static variables are stored in the method area, initialized when the class is loaded.
Differences: Constants are generally used publicly, constant values ​​are immutable, and static variable values ​​can be changed.


The above content is a bit of personal learning experience. If there are errors in the knowledge points, please leave a message to remind you, if there is any infringement content reminder, delete it immediately.

Guess you like

Origin blog.csdn.net/pf6668/article/details/108577341