Java Basics---Part 7

Table of Contents of Series Articles



1. What are the uses of final?

Final is also something that many interviews like to ask, but I think this question is boring. Usually it would be good if you can answer the following five points:
classes modified by final cannot be inherited.
Methods modified by final cannot be overridden and modified
by final. The variable cannot be changed. If the reference is modified, it means that the reference is immutable and the content pointed to by the reference is mutable.
For methods modified by final, the JVM will try to inline them to improve operating efficiency.
Constants modified by final will be compiled during compilation. stage will be stored in the constant pool.

In addition, the compiler has to follow two reordering rules for final fields:
writing a final field within the constructor, and subsequently removing the reference to the constructed object Assigning to a reference variable
cannot be reordered between the two operations. The initial reading of a reference to an object containing a final field and the subsequent initial reading of the final field cannot be
reordered.

2. What are the uses of static?

Everyone knows the two basic uses of the static keyword: static variables and static methods. That is, the variables/
methods modified by static belong to the static resources of the class and are shared by class instances.
In addition to static variables and static methods In addition, static is also used for static blocks, mostly for initialization operations:

Guess you like

Origin blog.csdn.net/pleaseprintf/article/details/133419799