Java Foundation only keyword final

Source: Internet
Author: User

Final keyword Brief

The final keyword is a very frequent and very high keyword in writing Java programs, and it is necessary to master the use of the final keyword if you want to write Java programs better. Let's take a look at where the final keyword can be decorated

    • final keyword decoration on a class
    • final keyword adornment on method
    • final keyword decoration on member variable
    • Final keyword adornment anonymous inner class method

      In view of my limited knowledge structure, I am grateful if I have any mistakes.
    • Final keyword decoration on a class
      when the final keyword is decorated on a class to mean that the class is not inheritable, and we are very familiar with the string, Integer, and so on are the final decorated class, "Java programming thought" A wrapper class that illustrates the basic types of string, such as the final decoration, does not want the string class to be inherited by another class to change the string class attribute.

        Public final class Finalkeywordtest {public Finalkeywordtest () {} public void Myfinalmethod () {System.ou  T.println ("this final"); }}class MyTest1 extends finalkeywordtest{//Compile error}  
    • Final keyword decoration on method
      When the final decoration is on a method, the method is not allowed to be overwritten (overridden). The idea of Java programming says that there are two reasons for using the final method. The first reason is to lock the method in case any inherited class modifies its meaning, and the second reason is efficiency. In earlier versions of Java implementations, the final method was converted to inline invocation. However, if the method is too large, you may not see any performance gains from inline calls. In the recent Java release, these optimizations do not need to be made using the final method.
      If you want to prohibit subclasses from overwriting a parent class method, it is best to use final to decorate it

public  class FinalKeyWordTest {  public FinalKeyWordTest(){}  public final void myFinalMethod(){      System.out.println("this final");  }}class MyTest1 extends FinalKeyWordTest{    @Override    public final void myFinalMethod(){//编译错误          System.out.println("this sub final");      }}
    • Final keyword decoration on member variable
      The final modified member variable is the most common way to use it, for a final modified variable:
      1. If it is a variable of the base data type, its value cannot be changed once it has been initialized.

      public  class FinalKeyWordTest {  public final int i; //编译错误}
public  class FinalKeyWordTest {  public final int i;  public FinalKeyWordTest(){      this.i = 1;  }}//编译是正确的

2. If you are a variable of a reference type, you can no longer point it to another object after it has been initialized.

public  class FinalKeyWordTest {   public static void main(String[] args) {      final MyTest1 m = new MyTest1();      m = new MyTest1(); //编译错误  }}
public  class FinalKeyWordTest {  public static void main(String[] args) {      final MyTest1 m = new MyTest1();      System.out.println(++ m.c);  }}class MyTest1{     int c = 0;}//正确
    • Final keyword adornments on anonymous inner class methods
      To be organized here, there is no more information to show why anonymous internal class method parameters must be final decoration, to have checked a few blog said are very general, some blog said anonymous internal class method parameter is final decoration It is not desirable to modify parameter data within a method to affect external invocation of class method execution. That's not quite comprehensive, we know that in Java, parameters are passed by value if they are basic data types, and if they are reference types, they are passed by reference. So there is no point in whether a parameter is a basic data type or a reference type, and if the parameter is the basic data type inner class method modifies the parameter contents, the outer class is not acquired at all, and if the argument is a reference data type, the inner class method can modify the property variable in the reference so that it can break the external calling class.

Java Foundation only keyword final

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.