1. Final class The final class cannot be inherited, so the member methods of the final class have no chance of being overwritten, and the default is final. When designing a class without a subclass, the implementation details of the class are not allowed to change, and it is assured that it will no longer be extended, then it is designed as the final class.
2. Final method If a method of a class does not allow child classes to overwrite, you can declare the method as the final method.There are two reasons for using the final method:First, lock the method to prevent any inherited classes from modifying its meaning and implementation. Second, efficient. The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.
3. Final variable (constant) A final decorated member variable represents a constant that cannot be changed once a value is given! ItsThere are three types of modified variables: Static variables, instance variables, and local variables, representing three kinds of constants, respectively. Once the final variable is initialized, the value can no longer be changed. In addition, when the final variable is defined, it can be declared without giving the initial value, which makesthe final type members in a class can be implemented differently depending on the object, while maintaining its constant characteristics. "The premise is thatThe compiler must ensure that final data is initialized before it is used"
4. Final parameters When the function parameter is the final type, the parameter is allowed to be read, but the parameter value cannot be modified.
Expand Learning Internal class access method parameters must be added final modifier
Public voidMrun(FinalString name) {
NewThread (NewRunnable () {
@Override
Public voidRun() {
Try{
Thread.Sleep( +);
}Catch(Interruptedexception e) {
// TODO auto-generated Catch block
E.printstacktrace ();
}
System. out. println (name);
}
}). Start ();
}
Reason for adding the final modifier to the name variable: At this point the life cycle of the inner class is at the member level, and the life cycle of the local variable is the actual method body, and so on. A special case occurs when the name variable is no longer in memory when the Mrun method finishes executing, but the name variable is still required in the thread. In order to eliminate this situation, Java has strict requirements for accessing local variables within the class, which must be decorated with the final keyword, where a copy of the local variable is kept in memory and accessed by the internal class.
A final modified variable means that only one instance is allowed to be instantiated
Finalfinalentity entity =NewFinalentity ("Wang June", at, NewStudent ());
LG. E (entity.tostring ());
Entity.Str="New Task";
Entity.Stu=NewStudent ();
Entity.Intvalue= -;
LG. E (entity.tostring ());
entity=NewFinalentity ("New str", About, NewStudent ());//Compilation error
The results of the operation are as follows: The final modified variable can only be instantiated once, but its internal member variables can be arbitrarily modified (even if the Finalentity class is final).
Public classfinalentity {
PublicStringStr;
PublicIntegerIntvalue;
PublicStudentStu;
PublicFinalentity (String str, Integer Intvalue, Student Stu) {
This.Str= str;
This.Intvalue= Intvalue;
This.Stu= Stu;
}
@Override
PublicStringtoString() {
return Super. toString () +"***" +Str+"***" +Intvalue+"***" +Stu. ToString ();
}
}
From for notes (Wiz)
Java's final keyword