Java关键字final使用详解

http://docs.oracle.com/javase/tutorial/java/IandI/final.html
Writing Final Classes and Methods
You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.

You might wish to make a method final if it has an implementation that should not be changed and it is critical to the consistent state of the object. For example, you might want to make the getFirstPlayer method in this ChessAlgorithm class final:

class ChessAlgorithm {
    enum ChessPlayer { WHITE, BLACK }
    ...
    final ChessPlayer getFirstPlayer() {
        return ChessPlayer.WHITE;
    }
    ...
}

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

Note that you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

从构造函数中调用的方法最好声明为final。若是构造调用了非final的方法,子类若是从新定义了那个方法,会带来意想不到的结果。
请注意你能够声明整个类是final的。被声明为final的类不会有子类。html


参考博客:
java

http://blog.csdn.net/andie_guo/article/details/12885885
编程


根据程序上下文环境,Java关键字final有“这是没法改变的”或者“终态的”含义,它能够修饰非抽象类、非抽象类成员方法和变量。你可能出于两种理解而须要阻止改变:设计或效率。
数组

  • final类不能被继承,没有子类,final类中的方法默认是final的。oracle

  • final方法不能被子类的方法覆盖,但能够被继承。函数

  • final成员变量表示常量,只能被赋值一次,赋值后值再也不改变。测试

  • final不能用于修饰构造方法。this

注意:父类的private成员方法是不能被子类方法覆盖的,所以private类型的方法默认是final类型的。

spa

一、final数据:

  • 一个永不改变的编译时常量。.net

  • 一个在运行时被初始化的值,而以后没法被改变;

  • 一个既是static又是final的域:是一段不能改变的存储空间;

final类型运用于数据:

  • 基本数据类型(int、double、char...)运用final时,使数值恒定不变;

  • 对象引用运用final时,final使得引用恒定不变,引用内部的数据若不是final型,能够进行修改。

  • 数组类型运用final时,final使得数组引用恒定不变,数组内部的数据若不是final型,能够进行修改。

final与static

  • final指明数据为一个常量,恒定没法修改;

  • static指明数据只占用一份存储区域;

public final class  FinalData {  
    private final int valueOne = 3;  
    private int valueTwo = 4;  
 
    private final int[] a = {1,2,3,4,5,6,7,8,9};  
    private int[] b = {1,2,3,4,5,6,7,8,9};  
    private static final int VAL_TWO = 3;  
      
 
    public static void main(String[] args) {  
        FinalData finalData = new FinalData(); 
        /*-----------基本类型测试------------------------------------*/  
//       finalData.valueOne = 4;//valueOne是常量,没法修改  
        finalData.valueTwo = 14;//valueTwo不是常量,能够修改  
          
        /*-----------对象类型测试------------------------------------*/  
//      finalData.v1 = new Value(5);//v1对象是final型常量,其引用是没法修改的。  
 
          
        /*-----------数组类型测试------------------------------------*/  
//      finalData.a = new int[3];//数组a是final型,没法修改a的引用  
        finalData.b = new int[13];//数组b不是final型,能够对其引用进行修改  
        for(int i=0;i<finalData.a.length;i++)   
            finalData.a[i]++;//数组a内部数据是int型,不是final型,能够修改  
          
        /*-----------static final类型测试------------------------------------*/  
//      finalData.VAL_TWO = 4;  
        //定义为private,只能被本类的方法调用;定义为static,则强调只有一份,且只被执行一次;定义为final,则说明它是一个常量,没法被修改。  
          
    }  
}

二、final方法

若是一个类不容许其子类覆盖某个方法,则能够把这个方法声明为final方法。

  • 把方法锁定,防止任何继承类修改它的意义和实现。

  • 高效。编译器在遇到调用final方法时候会转入内嵌机制,大大提升执行效率。

public class FinalDemo {  
  
    public void f(){  
        System.out.println("FianlDemo.f()");  
    }  
    public final void g(){  
        System.out.println("FianlDemo.g()");  
    }  
}

继承类

public class FinalOverriding extends FinalDemo{

    public void f(){
        System.out.println("FinalOverriding.f()");
    }
    
//    public void g(){//没法覆盖父类的final方法g()
//        System.out.println("FinalOverriding.g()");
//    }
}

三、final类

final类不能被继承,所以final类的成员方法没有机会被覆盖,默认都是final的。在设计类时候,若是这个类不须要有子类,类的实现细节不容许改变,而且确信这个类不会载被扩展,那么就设计为final类。例如:java.lang.String

四、参考:

  • 《Java编程思想(美)Bruce Eckel著 陈昊鹏 译》

相关文章
相关标签/搜索