Java Final keyword

Source: Internet
Author: User

In the Java keyword,static and final are two of the keywords we have to master. Unlike other keywords, they are used in a variety of ways, and can improve the performance of the program and optimize the structure of the program in a certain environment.

1.FIANL variables

The FIANL keyword can be used to declare a variable, and once the variable is set, the value of the variable cannot be changed, and the defined variable must be assigned at the time of declaration. Once an object reference is decorated to final, it can only point to an object and cannot be changed to point to another object. To get an insight into the final keyword, let's look at the following example.

ClassValue{
IntV
PublicValue(IntV{
THIS.V = v;
}
}
PublicClassFinaltest{
FinalIntF1 =1;
FinalIntF2;
PublicFinaltest(){
F2 =2;
}
PublicStaticvoidMain(string[] args) {
        final   int value1 =  1;
        //value1 = 4;
        final  double value2;
       value2 =  2.0;
        final value value3 =  new value ( 1);
       VALUE3.V =  4;
   }
}

In the example above, let's take a look at some of the final modified data in the main method, after assigning the initial value to value1, we can no longer modify the value of value1, the final keyword plays a constant role. From value2 we can see that the final modified variable can not be assigned a value at the time of Declaration, that is, it can be declared first and then assigned. Value3 a reference variable, here we can see the final modifier reference variable, only to qualify the reference variable reference can not be changed, that is, you cannot refer to another value object again, but the value of the referenced object can be changed.

2.final method

The final keyword is used to decorate the method, which indicates that the method cannot be overwritten. This use is mainly from the design perspective, that is, explicitly tell other programmers who might inherit the class, do not want them to overwrite this method. This approach is easy to understand, however, there is a little connection between the private and final keywords, which is that all private methods in the class are implicitly specified as final and cannot be overwritten because the private method cannot be used outside the class. See the following example.

Classparents{
PrivateFinalvoidDoit){
System.Out.println ("Parent class. Doit ()");
}
Final voidDoit2 (){
System.Out.println ("Parent class. Doit2 ()");
}
PublicvoidDOIT3 (){
System.Out.println ("Parent class. Doit3 ()");
}
}
ClassSubExtendsparents{
PublicFinalvoidDoit){Define a doit () method in a subclass
System.Out.println ("Subclass. Doit ()");
}
/* Final void Doit2 () {
System.out.println ("subclass. Doit2 ()"); Doit2 () in sub cannot overwrite Doit2 () in parents
} */
PublicvoidDOIT3 (){
System.Out.println ("Subclass. Doit3 ()");
}
}
Public class finalmethod{
Public static void main (string[] args) {
Sub t=New Sub (); //instantiation of a subclass object
T.doit (); //Call the doit () method of the Subclass object
Parents p=t; //Subclass object is transformed upward into a parent class object
//p.doit (); The doit () method is defined as private and the subclass cannot access
P.doit2 (); //Call the Doit2 () method of the parent class object
P.doit3 (); //overwrite, overwrite must satisfy an object to be transformed upward into its basic type and call the same method
}
}

As can be seen from this example, the final method cannot be overwritten, overwriting must satisfy an object to be transformed upward to its basic type and call the same method such a condition, for example in the sub-method, parents p=t; The subclass object is transformed upward into a parent class object, and the object P can only call the Doit3 () method that is normally overwritten, but cannot call the doit () method.

3.final class

Knowing the other uses of the final keyword, it is easy to think of the effect of using the final keyword to decorate the class, which is that a final decorated class cannot be inherited. If a class is set to the final type, all methods in the class are implicitly set to final form, and member variables in the final class can be set to final or non-final types. See the following example.

final class FinalClass{
 int a=3;
 public static void main(String[] args){
   FinalClass t=new FinalClass();
       t.a++;
   System.out.println(t.a);  //输出a=4
 }
}

搜索公众号“程序员考拉”,欢迎关注!

Java Final keyword

Related Article

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.