final basic use

1 Basic introduction

final Chinese meaning: final, final.
final can modify classes, properties, methods and local variables.
In some cases, programmers may use final if they have the following requirements:

1) When you don't want the class to be inherited, you can use final modification.

2) When you do not want a method of the parent class to be overridden/overridden by the subclass, you can use the final keyword to modify it.

[Case Demonstration: Access Modifier final Return Type Method Name]

3) When the value of a property of the class is not expected to be modified, it can be modified with final. [Case Demonstration: publicfinal double TAX_RATE=0.08]

4) When you do not want a local variable to be modified, you can use final modification [case demonstration: final double TAX RATE=0.08 )

public class Final01 {
    public static void main(String[] args) {
        E e = new E();
        //e.TAX_RATE = 0.09;
    }
}

//If we require that class A cannot be inherited by other classes
//Class A can be modified with final
final class A { }

//class B extends A {}

class C {
    //If we require that hi cannot be overridden by subclasses
    //You can use final to modify the hi method
    public final void hi() {}
}
class D extends C {
//    @Override
//    public void hi() {
//        System.out.println("Override the hi method of class C..");
//    }
}

//When you do not want the value of a property of a class to be modified, you can use final modification
class E {
    public final double TAX_RATE = 0.08;
}

//When you do not want a local variable to be modified, you can use final modification
class F {
    public void cry() {
        //In this case, NUM is also called a local constant
        final double NUM = 0.01;
        //NUM = 0.9;
        System.out.println("NUM=" + NUM);
    }
}

2 final use precautions

  1. The properties modified by final are also called constants, which are generally named with XX_XX_XX;

  2. The final modified attribute must be assigned an initial value when it is defined, and cannot be modified later. The assignment can be in one of the following locations [select a location to assign an initial value]:
    ①When defining: such as public final double TAX_RATE=0.08;

​ ②In the constructor;

​ ③In the code block.

3) If the final modified property is static, the initialized location can only be

​ ①When defining,

​ ②In the static code block, you cannot assign values ​​in the constructor.

  1. final classes cannot inherit, but classes can instantiate objects. [Class A2]

5) If the class is not a final class, but contains a final method, the method cannot be overridden, but it can be inherited. [Class A3]

6) In general, if a class is already a final class, there is no need to modify the method as a final method.

  1. final cannot modify constructors (ie constructors);

  2. final and static are often used together, which is more efficient and will not cause class loading because the underlying compiler does optimization.

9) Wrapper class (Integer,Double,Float, Boolean, etc. are all final),String is also a final class.

public class FinalDetail01 {
    public static void main(String[] args) {
        CC cc = new CC();

        new EE().cal();
    }
}

class AA {
    /*
    1. When defining: such as public final double TAX_RATE=0.08;
    2. in the constructor
    3. in a code block
     */
    public final double TAX_RATE = 0.08;//1. Assignment when defining
    public final double TAX_RATE2 ;
    public final double TAX_RATE3 ;

    public AA() {//assignment in constructor
        TAX_RATE2 = 1.1;
    }
    {//assignment in code block
        TAX_RATE3 = 8.8;
    }
}

class BB {
    /*
    If the final modified property is static, the location of initialization can only be
    1 When defined 2 in a static code block can not be assigned in the constructor.
     */
    public static final double TAX_RATE = 99.9;
    public static final double TAX_RATE2 ;

    static {
        TAX_RATE2 = 3.3;
    }


}

//final classes cannot inherit, but objects can be instantiated
final class CC { }


//If the class is not final, but contains a final method, the method cannot be overridden, but it can be inherited
//That is, the mechanics of inheritance are still respected.
class DD {
    public final void cal() {
        System.out.println("cal()method");
    }
}
class EE extends DD { }

public class FinalDetail02 {
    public static void main(String[] args) {
        System.out.println(BBB.num);

        //Wrapper class, String is a final class and cannot be inherited

    }
}

//final and static are often used together, which is more efficient and will not cause class loading. The underlying compiler does optimization processing
class BBB {
    public final static  int num = 10000;
    static {
        System.out.println("BBB static code blocks are executed");
    }
}
final class AAA{
    //Generally speaking, if a class is already final, there is no need to make the method final.
    //public final void cry() {}
}

3 exercises (error prone)

Tags: Java

Posted by Zup on Sun, 02 Oct 2022 16:37:27 +1030