0 , Overview
final keyword indicates that is immutable;
The following were the described attributes (fields), the method, the class;
1 , the attribute (or field), denotes a constant
In the final declaration attribute (or field), a denotes a constant, there are two methods to initialize, a declaration is initialized directly, like this static constant, it is determined at compile time; 2 is initialized in the constructor, this is its value is determined at runtime; sample code is as follows:
public class Man { Final String the Name = "John Doe"; // 1, constant declaration initialized with static Final String EnName; / ** * constructor * / public Man () { EnName = "Anson"; // 2, in the constructor initializes, its value is determined until run } }
2 , Final method, the method can not be covered represents
The main role of the final method has two aspects: one is to prevent any derived class override methods . 2 is a more efficient program execution . After a method is set to final, the compiler will ignore conventional method to insert the code to perform the method call mechanism taken (the independent variable onto the stack; skip method code and execute it; jump back; clear the stack arguments; and finally processing the return value). It will replace the method call with a copy of the method within the body of the actual code. This can avoid the overhead of method calls to a certain extent.
Sample code is as follows:
class Man { / ** * Final methods can not override * / public Final void the Run () { // ...} }
3 , Final class that represents the class can not be inherited
Final declaration of the class, the class can not be inherited represents; sample code is as follows:
final class Human { //... }
000-static
About static, is better understood, do not write code example, there are four main uses: summarized as follows:
- Used to modify a member variable, it becomes a member of the class, in order to achieve all the objects for sharing the member;
- The method used to modify members, to turn it into a class method, you can directly use the " class name . Method name " way calling, commonly used tools;
- Use static block, a plurality of members based on initialisation, to initialize static member;
- Static bag usage guide, the class method directly into the current class, which directly use the " method name " to call the class method is more convenient, such as:
-
import static com.anson.utils.PythonHelper.*;