Keyword final in Java

There are three places that final can be used:

  1. data
  2. method
  3. class

final data

compile-time constant
In the case of compile-time constant, the compiler is allowed to "fold" the constant value into any calculations in which it is used. (That means the calculation can be completed during compile time)
In Java, these constants must be primitive and must be expressed with the final keyword. You also need to give it a value when it is defined.

A field that is final and static has only one piece of storage that cannot be changed.
When final is used for an object, that doesn't mean the object cannot be changed. It means that the reference is constant. This restriction also includes arrays, which are also objects.

Example

//: reusing/FinalData.java
// The effect of final on fields.import java.util.*;
import static net.mindview.util.Print.*;
class Value {
 int i;
 // Package access
 public Value(int i) { this.i = i; }
}
public class FinalData {
 private static Random rand = new Random(47);
 private String id;
 public FinalData(String id) { this.id = id; }
 // Can be compile-time constants:
 private final int valueOne = 9;
 private static final int VALUE_TWO = 99;
 // Typical public constant:
 public static final int VALUE_THREE = 39;
 // Cannot be compile-time constants:
 private final int i4 = rand.nextInt(20);
 static final int INT_5 = rand.nextInt(20);
 private Value v1 = new Value(11);
 private final Value v2 = new Value(22);
 private static final Value VAL_3 = new Value(33);
 // Arrays: private final int[] a = { 1, 2, 3, 4, 5, 6 };
 public String toString() {
 return id + ": " + "i4 = " + i4 + ", INT_5 = " + INT_5;
 }
public static void main(String[] args) {
    FinalData fd1 = new FinalData("fd1");
    //! fd1.valueOne++; // Error: can’t change value
    fd1.v2.i++; // Object isn’t constant!
    fd1.v1 = new Value(9); // OK -- not final
    for(int i = 0; i < fd1.a.length; i++)
      fd1.a[i]++; // Object isn’t constant!
    //! fd1.v2 = new Value(0); // Error: Can’t
    //! fd1.VAL_3 = new Value(1); // change reference
    //! fd1.a = new int[3];
    print(fd1);
    print("Creating new FinalData");
    FinalData fd2 = new FinalData("fd2");
    print(fd1);
    print(fd2);
  }
} /* Output:
fd1: i4 = 15, INT_5 = 18
Creating new FinalData
fd1: i4 = 15, INT_5 = 18
fd2: i4 = 13, INT_5 = 18
*///:~```

valueOne and VALUE_TWO are compile-time constants because they are primitives with compile-time values.
VALUE_THREE is more typical: **public** so it's usable outside the package, **static** to emphasize there is only one, and **final** to say that it's constant.

Note that something final doesn't mean its value is known at compile time, such as i4 and INT_5. i4 and INT_5 also shows the difference between making a final value **static** or **no-static**.
You can see that i4 can be different between different instances, while INT_5 won't change since all the instances share the same INT_5.

**final arguments**
You can make arguments final, this means inside the method you cannot change what the arguments reference to. This feature is primarily used to pass data to anonymous inner classes.

## final method
Put a “l(fā)ock” on the method to prevent any inheriting class from changing its meaning.

**private vs final**
All the private method is implicitly declared as "final".
If you try to override the private method, compiler won't say it's wrong. However, you just add a new method to your class, not overriding.

## final class
When you say that an entire class is final (by preceding its definition with the final keyword), you state that you don’t want to inherit from this class or allow anyone else to do so. 




最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末住拭,一起剝皮案震驚了整個(gè)濱河市贮尖,隨后出現(xiàn)的幾起案子匀归,更是在濱河造成了極大的恐慌癌淮,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,378評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件许师,死亡現(xiàn)場離奇詭異洋闽,居然都是意外死亡瓤球,警方通過查閱死者的電腦和手機(jī)溺忧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門咏连,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鲁森,你說我怎么就攤上這事捻勉。” “怎么了刀森?”我有些...
    開封第一講書人閱讀 152,702評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長报账。 經(jīng)常有香客問我研底,道長,這世上最難降的妖魔是什么透罢? 我笑而不...
    開封第一講書人閱讀 55,259評(píng)論 1 279
  • 正文 為了忘掉前任榜晦,我火速辦了婚禮,結(jié)果婚禮上羽圃,老公的妹妹穿的比我還像新娘乾胶。我一直安慰自己,他們只是感情好朽寞,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,263評(píng)論 5 371
  • 文/花漫 我一把揭開白布识窿。 她就那樣靜靜地躺著,像睡著了一般脑融。 火紅的嫁衣襯著肌膚如雪喻频。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,036評(píng)論 1 285
  • 那天肘迎,我揣著相機(jī)與錄音甥温,去河邊找鬼。 笑死妓布,一個(gè)胖子當(dāng)著我的面吹牛姻蚓,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播匣沼,決...
    沈念sama閱讀 38,349評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼狰挡,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起圆兵,我...
    開封第一講書人閱讀 36,979評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤跺讯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后殉农,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刀脏,經(jīng)...
    沈念sama閱讀 43,469評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,938評(píng)論 2 323
  • 正文 我和宋清朗相戀三年超凳,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了愈污。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,059評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡轮傍,死狀恐怖暂雹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情创夜,我是刑警寧澤杭跪,帶...
    沈念sama閱讀 33,703評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站驰吓,受9級(jí)特大地震影響涧尿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜檬贰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,257評(píng)論 3 307
  • 文/蒙蒙 一姑廉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧翁涤,春花似錦桥言、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至鸳粉,卻和暖如春倦西,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背赁严。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評(píng)論 1 262
  • 我被黑心中介騙來泰國打工扰柠, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人疼约。 一個(gè)月前我還...
    沈念sama閱讀 45,501評(píng)論 2 354
  • 正文 我出身青樓卤档,卻偏偏與公主長得像,于是被迫代替她去往敵國和親程剥。 傳聞我的和親對(duì)象是個(gè)殘疾皇子劝枣,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,792評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,389評(píng)論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,433評(píng)論 5 6
  • 這個(gè)周末又不能見面了 我這周要回家和家里人過生日汤踏,昂還是帶著使命去的,一定要把崽崽接回來舔腾,小范范說想崽崽了溪胶。 其實(shí)...
    RYlian閱讀 138評(píng)論 0 0
  • 我大學(xué)生活的地方哗脖,對(duì)我而言是一個(gè)槽點(diǎn)滿滿的城市,在這樣一座尷尬的城中扳还,有一輛熱鬧非凡的公交才避,我們暫稱其為一路...
    哭死的小人兒閱讀 156評(píng)論 0 0
  • 今天又從安娜處學(xué)到幾點(diǎn) 1、要學(xué)會(huì)讓你的員工在工作中找到自己的價(jià)值氨距,讓他有提升自我的渠道桑逝,不要以為讓自己的員工游手...
    陳佩榆閱讀 127評(píng)論 0 0