Java's final keyword

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.