這是一個(gè)以前從沒仔細(xì)想過的問題——最近在閱讀Java Puzzlers摧阅,發(fā)現(xiàn)其大量使用了“域”這個(gè)詞汰蓉,這個(gè)詞個(gè)人很少見到,在這本書中倒是時(shí)常出現(xiàn)棒卷,所以在好奇心的驅(qū)使下搜索了一下相關(guān)的內(nèi)容顾孽,順便復(fù)習(xí)了一下基礎(chǔ)祝钢,最后整理如下。
先說一下 field 和 variable 之間的區(qū)別:
class variables and instance variables are fields while local variables and parameter variables are not. All fields are variables.
成員變量(field)是指類的數(shù)據(jù)成員若厚,而方法內(nèi)部的局部變量(local variable)拦英、參數(shù)變量(parameter variable)不能稱作 field。field 屬于 variable测秸,也就是說 variable 的范圍更大疤估。
術(shù)語解釋:
-
域或字段、實(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)描述上面的定義更佳显晶。
- 成員變量與特定的對象相關(guān)聯(lián),只能通過對象(new 出)訪問壹士。
- 聲明在類中吧碾,但不在方法或構(gòu)造方法中。
- 如果有多個(gè)對象的實(shí)例墓卦,則每一個(gè)實(shí)例都會(huì)持有一份成員變量倦春,實(shí)例之間不共享成員變量的數(shù)據(jù)。
- 作用域比靜態(tài)變量小落剪,可以在類中或者非靜態(tài)方法中使用以及通過生成實(shí)例對象使用睁本。(訪問限制則不可用)
- JVM 在初始化類的時(shí)候會(huì)給成員變量賦初始值。
Example:
public class FieldTest { private int xValue; // xValue is a field public void showX() { System.out.println("X is: " + xValue); } }
-
類字段忠怖、靜態(tài)字段呢堰、靜態(tài)變量(class variable, static field, staic variable)
- 使用 static 修飾的字段,一般叫做靜態(tài)變量凡泣。
- 聲明在類中枉疼,但不在方法或構(gòu)造方法中。
- 多個(gè)實(shí)例對象共享一份靜態(tài)變量
- JVM在準(zhǔn)備類的時(shí)候會(huì)給靜態(tài)變量賦初始值鞋拟。
- 作用域最大骂维,類中都可以訪問,或通過 類名.變量名 的方式調(diào)用(訪問限制則不可用)贺纲。
Example:
System.out.println(Integer.MAX_VALUE);
-
局部變量(local variable)
定義在一個(gè)區(qū)塊內(nèi)(通常會(huì)用大括號包裹)航闺,區(qū)塊外部無法使用的變量。- 定義在一個(gè)區(qū)塊內(nèi)(通常會(huì)用大括號包裹),沒有訪問修飾符潦刃,區(qū)塊外部無法使用的變量侮措。
- 沒有默認(rèn)值,所以必須賦初始值
- 生命周期即為方法的生命周期
Example:
if(x > 10) { String local = "Local value"; }
-
參數(shù)(input parameter, parameter (variable), argument)
這個(gè)就不多說了乖杠,要注意的是 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)
不可變量、常量(final variable, constant)
即為使用 final 關(guān)鍵詞修飾的變量略荡。不可變量屬于成員變量。-
成員(member)
A field or method of a class. Unless specified otherwise, a member is not static.
指的是類中非靜態(tài)的成員變量或方法歉胶。(用法同field)
-
屬性(property)
Characteristics of an object that users can set, such as the color of a window.
可以被用戶設(shè)置或獲取的對象特征即為屬性汛兜。
POJO 或 JavaBean 中的成員變量也稱作屬性(具有set、getter方法)通今。
最后粥谬,總結(jié)一下國內(nèi)目前的慣用法(英文取其一,序號對應(yīng)上文):
- field -> 成員變量辫塌, instance variable / non-static field -> 實(shí)例變量/非靜態(tài)變量
- class variable -> 靜態(tài)變量
- local variable -> 本地變量
- input parameter -> 參數(shù)
- final variable -> 常量
- member -> 成員(用法同field)
- 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