In Java programming, we may sometimes hope that some data cannot be changed, and that this is where the final comes into play. Final is Java's
off The key word that it represents is that this part cannot be modified. There are two reasons why you don't want to be changed: efficiency, design. There are three scenarios for final use: variable
Volume (the final variable is a constant), Square law, class.
A final keyword
Final is a reserved keyword in Java and can declare member variables, methods, classes, and local variables. Once you have made the reference statement final, you
will be cannot change this reference, the compiler will check the code and if you try to initialize the variable again, compile The compiler will report a compilation error.
Two final variable
A final variable is always declared as final for a member variable or a local variable (in a method or a variable in a code block, called a local variable). Final
Variables are often used with the static keyword as constants. Declaring a final variable helps the optimizer make better optimization decisions, because if the compiler knows
The value of the field does not change, it is safe to cache the value in the register. The final variable also provides the amount by having the compiler force the field to be read-only
Level of security outside. Final is often used with static to declare constants, and you'll see how final improves application performance.
When you use the final keyword to modify a variable, you should be aware of:
1) in Java, normal variables can be initialized by default, but variables of the final type must be explicitly initialized.
2) The final member variable can and can only be initialized once.
3) The final member variable must be initialized at the time of declaration (which is assigned directly to the final variable definition) or in the constructor, but not elsewhere
Be initialized.
Here is an example of the final variable, The code is as follows:
public class Test{final int b1 = 1;//defined string constant final int b2;//final member variable B2 we use constructor initialization//constructor Test (int b) {this.b2 = b;} public static void Main (string[] args) {test test = new Test (2); test.b1 = 5;//compilation error, final member variable can only be initialized once System.out.println ( TEST.B1); System.out.println (TEST.B2);}}
Compile times wrong:
The final variable is read-only.
Three final method
Final can also declare methods. The method is preceded by the final keyword, which means that the method cannot be overridden by a quilt class. If you think a method
function is complete enough, and you can declare this method final if you don't need to change it in subclasses. The final method is faster than the non-final method because the
The translation is statically bound and does not need to be dynamically bound at run time.
The final method of the parent class is not covered by the quilt class, that is, the subclass is not able to exist the same way as the parent class.
The following is an example of the final method, as follows:
public class Test{public static void Main (string[] args) {Student S1 = new Student (); System.out.println (S1.getname ());}} Class Person{//final method Public final String GetName () {return ' person '; }} Class Student extends person{ //Overriding the GetName method in the parent class person the public final String GetName () {return "Student"; }}
Compile error message:
Four final class
A class that is decorated with the final keyword is called the final class. The final class is generally functionally complete and cannot be inherited. There are many classes in Java that are final,
such as String, Interger, and other wrapper classes.
If a class is finished with a final decoration, it indicates that the class is the final class, and it does not want or allow others to inherit it. In the design of security or other
Cause, we don't allow any changes to the class, and we don't want it to have subclasses, so we can use final to decorate the class.
For a final decorated class, its member variables can be final or non-final. If it is defined as final, then the rules for final data are also
Fit for it. And its methods are automatically added to final, because the final class cannot be inherited, so this is the default.
The following is an instance of the final class, with the following code:
Class Person{//final method Public final String GetName () {return ' person '; }} Final class Student extends Person{int id = 123;public int getId () {return id; }} Class S extends student{}
Compile error:
Five The benefits of the final keyword
The following summarizes some of the benefits of using the final keyword
1) The final keyword improves performance. Both the JVM and the Java application cache the final variable.
2) The final variable can be shared securely in a multithreaded environment without the need for additional synchronization overhead.
3) Using the final keyword, the JVM optimizes methods, variables, and classes.
4) Create an immutable class to use the final keyword. An immutable class means that its objects cannot be changed once they have been created. String is an immutable class
Represent. Immutable classes have many benefits, such as their objects are read-only, can be shared securely in multithreaded environments, without additional synchronization overhead, etc.
and other benefits.
Six final important points of knowledge summary
1) The final keyword can be used for member variables, local variables, methods, and classes.
2) The final member variable must be initialized at the time of declaration or initialized in the constructor, or a compilation error will be reported.
3) cannot assign a value to the final variable again.
4) Local variables must be assigned at the time of declaration.
5) All variables in an anonymous class must be final variables.
6) The final method cannot be overridden.
7) The final class cannot be inherited.
8) The final keyword differs from the finally keyword, which is used for exception handling.
9) The final keyword is easy to confuse with the Finalize () method, which is the method defined in the object class, which is called by the JVM before garbage collection.
10) All the variables declared in the interface are final.
Final and abstract The two keywords are anti-related, and the final class cannot be abstract.
The final method is bound in the compile phase, called static binding.
13) No final variable is initialized at declaration time called a blank final variable (blank final variable), they must be initialized in the constructor, or
Initialize with this (). If you do not, the compiler will say "final variable (variable name) needs to be initialized".
14) Declaring classes, methods, and variables as final can improve performance, so the JVM has an opportunity to estimate and then optimize
15) According to the Java Code convention, the final variable is constant, and the pass is often capitalized:private final int COUNT = ten
16) For a collection object declaration to final refers to a reference that cannot be changed, but you can add, delete, or change the content to it. For example :
Private final List Loans = new ArrayList (); List.add ("Home loan"); Validlist.add ("personal loan"); Validloans = new Vector (); Not valid
The above code is not very understanding?
We already know what the final variable, final method, and final class are. Use final when necessary to write faster, better code.
Javase Getting Started learning 22:java Object-oriented final keyword