Java 中 field 和 variable 區(qū)別及相關(guān)術(shù)語(yǔ)解釋

這是一個(gè)以前從沒(méi)仔細(xì)想過(guò)的問(wèn)題——最近在閱讀Java Puzzlers,發(fā)現(xiàn)其大量使用了“域”這個(gè)詞,這個(gè)詞個(gè)人很少見(jiàn)到,在這本書(shū)中倒是時(shí)常出現(xiàn),所以在好奇心的驅(qū)使下搜索了一下相關(guān)的內(nèi)容,順便復(fù)習(xí)了一下基礎(chǔ),最后整理如下。

先說(shuō)一下 field 和 variable 之間的區(qū)別:

class variables and instance variables are fields while local variables and parameter variables are not. All fields are variables.

成員變量(field)是指類(lèi)的數(shù)據(jù)成員,而方法內(nèi)部的局部變量(local variable)、參數(shù)變量(parameter variable)不能稱(chēng)作 field。field 屬于 variable,也就是說(shuō) variable 的范圍更大。

術(shù)語(yǔ)解釋?zhuān)?/h2>
  1. 域或字段、實(shí)例變量、成員變量(field, instance variable, member variable, non-static field)

    field: A data member of a class. Unless specified otherwise, a field is not static.

    • 非 static 修飾的變量。
    • 雖然有如上定義,但是一般在使用時(shí),成員變量(field)包括 instance variable 和 class variable。為了區(qū)分,個(gè)人認(rèn)為,用實(shí)例變量/非靜態(tài)變量(instance variable / non-static field)描述上面的定義更佳。
    • 成員變量與特定的對(duì)象相關(guān)聯(lián),只能通過(guò)對(duì)象(new 出)訪問(wèn)。
    • 聲明在類(lèi)中,但不在方法或構(gòu)造方法中。
    • 如果有多個(gè)對(duì)象的實(shí)例,則每一個(gè)實(shí)例都會(huì)持有一份成員變量,實(shí)例之間不共享成員變量的數(shù)據(jù)。
    • 作用域比靜態(tài)變量小,可以在類(lèi)中或者非靜態(tài)方法中使用以及通過(guò)生成實(shí)例對(duì)象使用。(訪問(wèn)限制則不可用)
    • JVM 在初始化類(lèi)的時(shí)候會(huì)給成員變量賦初始值。

    Example:

    public class FieldTest  {
        private int xValue; // xValue is a field
    
        public void showX() {
            System.out.println("X is: " + xValue);
        }
    }
    
  2. 類(lèi)字段、靜態(tài)字段、靜態(tài)變量(class variable, static field, staic variable)

    • 使用 static 修飾的字段,一般叫做靜態(tài)變量。
    • 聲明在類(lèi)中,但不在方法或構(gòu)造方法中。
    • 多個(gè)實(shí)例對(duì)象共享一份靜態(tài)變量
    • JVM在準(zhǔn)備類(lèi)的時(shí)候會(huì)給靜態(tài)變量賦初始值。
    • 作用域最大,類(lèi)中都可以訪問(wèn),或通過(guò) 類(lèi)名.變量名 的方式調(diào)用(訪問(wèn)限制則不可用)。

    Example:

    System.out.println(Integer.MAX_VALUE);
    
  3. 局部變量(local variable)
    定義在一個(gè)區(qū)塊內(nèi)(通常會(huì)用大括號(hào)包裹),區(qū)塊外部無(wú)法使用的變量。

    • 定義在一個(gè)區(qū)塊內(nèi)(通常會(huì)用大括號(hào)包裹),沒(méi)有訪問(wèn)修飾符,區(qū)塊外部無(wú)法使用的變量。
    • 沒(méi)有默認(rèn)值,所以必須賦初始值
    • 生命周期即為方法的生命周期

    Example:

    if(x > 10) {
        String local = "Local value";
    }
    
  4. 參數(shù)(input parameter, parameter (variable), argument)

    這個(gè)就不多說(shuō)了,要注意的是 argument 和 parameter 的區(qū)別(下文)。
    另外,Oracle 官方文檔中將參數(shù)分為了構(gòu)造參數(shù)、方法參數(shù)和異常參數(shù)三部分。

    Example:

    public class Point {
    private int xValue;
        public Point(int x) {
            xValue = x;
       }
    
        public void setX(int x) {
            xValue = x;
        }
    }
    

    Strictly speaking, a parameter is a variable within the definition of a method. An argument would be the data or actual value which is passed to the method. An example of parameter usage: int numberAdder(first, second) An example of argument usage: numberAdder(4,2)

  5. 不可變量、常量(final variable, constant)
    即為使用 final 關(guān)鍵詞修飾的變量。不可變量屬于成員變量。

  6. 成員(member)

    A field or method of a class. Unless specified otherwise, a member is not static.

    指的是類(lèi)中非靜態(tài)的成員變量或方法。(用法同field)

  7. 屬性(property)

    Characteristics of an object that users can set, such as the color of a window.

    可以被用戶(hù)設(shè)置或獲取的對(duì)象特征即為屬性。
    POJO 或 JavaBean 中的成員變量也稱(chēng)作屬性(具有set、getter方法)。

最后,總結(jié)一下國(guó)內(nèi)目前的慣用法(英文取其一,序號(hào)對(duì)應(yīng)上文):

  1. field -> 成員變量, instance variable / non-static field -> 實(shí)例變量/非靜態(tài)變量
  2. class variable -> 靜態(tài)變量
  3. local variable -> 本地變量
  4. input parameter -> 參數(shù)
  5. final variable -> 常量
  6. member -> 成員(用法同field)
  7. property -> 屬性

參考資料:

fields-vs-variables-in-java
http://docs.oracle.com/javase/tutorial/information/glossary.html
https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.12

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容