"Can not change the design" - final keyword in Java Discussion

In Java, final keyword can be used to modify the class, variables (including local variables and member variables), the method, from the following three aspects are described.

final method

When a method is modified final, it shows that this method can not be overridden by subclasses.

The following program attempts to rewrite the final approach will lead to compilation errors.

public class Dinosaur {
    public final void test() {
    }
}

public class Further extends Dinosaur {
    // 下面代码会编译错误,不能重写final方法
    public void test() {
    }
}

In particular, all the private methods the class are designated as being implicitly final.

final class

When a class is modified final, it indicates that the class can not be inherited. If a class tries to inherit a final class will compile errors.

// 定义一个Dinosaur类,并声明为final的
public final class Dinosaur {
    //...
}

// 当Further试图继承Dinosaur,编译会不通过
public class Further extends Dinosaur {

}

Since the final class can not be inherited, the final classes all methods implicitly designated as final. You can also give these final class method plus fianl modifier, but doing so does not make any sense.

Like we used the String class, it is to be designed to be final, and it can not have subclasses.

final variables

Variables into local variables and member variables, are explained below.

  • final member variables

When a final member variable is modified, this variable indicates that the initialization is completed (instance variables: either the initial value specified in the definition of the instance variable, or the initial value specified in the block or the normal initialization constructor; class variables: either the definition of the class specifying the initial variable value, or the initial value specified in the static initialization block), it can not be changed.

The following code demonstrates final modification program member variable results.

public class FinalVariableTest {

    // 定义成员变量时指定初始化值
    private final int a = 6;

    // 定义成员变量时未指定初始化值,将在构造器中进行初始化
    private final String s;

    public FinalVariableTest() {
        // 初始化s的值,如果不初始化s的值,将会编译错误,final修饰的成员变量必须由程序显式地指定初        // 始化值
        this.s = "程序员技术之瞳";

        // ERROR,这里试图给a重新赋值,将会编译错误
        a = 7;
    }

}
  • final local variables

If the final modified local variables in the declaration has been assigned the initial value, after the code can not be re-assigned.

public void test() {
    // 这里定义一个final的局部变量,并赋了初始值1
    final int a = 1;

    // ERROR,试图重新赋值,则会编译错误
    a = 2;
}

If the final modified local variables when the initial value is not specified in the statement, the code can be later assigned to the variable, but can only be assigned once .

public void test() {
    // 这里定义一个final的局部变量,但没有指定初始值
    final int a;

    // 给变量赋值为1
    a = 1;

    // ERROR,这里若试图给a重新赋值,则会编译错误
    a = 2;
}

final modification of the basic data types of variables and reference type variable difference

When the final modification of the basic data types is a variable that once the initialization value, the value will not be changed.

When the final modification of the reference data type is variable, since the variable is stored in an address value, to ensure that the final variable does not refer to another object, the contents of the object may be changed.

Guess you like

Origin www.cnblogs.com/sum-41/p/11281764.html