property field java_Java 中 field 和 variable 区别及相关术语解释
原標題:Java 中 field 和 variable 區別及相關術語解釋
先說一下 field 和 variable 之間的區別:
class variables and instance variables are fields while local variables and parameter variables are not. All fields are variables.
成員變量(field)是指類的數據成員,而方法內部的局部變量(local variable)、參數變量(parameter variable)不能稱作 field。field 屬于 variable,也就是說 variable 的范圍更大。
術語解釋:
域或字段、實例變量、成員變量(field, instance variable, member variable, non-static field)
field: A data member of a class. Unless specified otherwise, a field is not static.
非 static 修飾的變量。
雖然有如上定義,但是一般在使用時,成員變量(field)包括 instance variable 和 class variable。為了區分,個人認為,用實例變量/非靜態變量(instance variable / non-static field)描述上面的定義更佳。
成員變量與特定的對象相關聯,只能通過對象(new 出)訪問。
聲明在類中,但不在方法或構造方法中。
如果有多個對象的實例,則每一個實例都會持有一份成員變量,實例之間不共享成員變量的數據。
作用域比靜態變量小,可以在類中或者非靜態方法中使用以及通過生成實例對象使用。(訪問限制則不可用)
JVM 在初始化類的時候會給成員變量賦初始值。
Example:
類字段、靜態字段、靜態變量(class variable, static field, staic variable)
使用 static 修飾的字段,一般叫做靜態變量。
聲明在類中,但不在方法或構造方法中。
多個實例對象共享一份靜態變量
JVM在準備類的時候會給靜態變量賦初始值。
作用域最大,類中都可以訪問,或通過 類名.變量名 的方式調用(訪問限制則不可用)。
Example:
局部變量(local variable)
定義在一個區塊內(通常會用大括號包裹),區塊外部無法使用的變量。
定義在一個區塊內(通常會用大括號包裹),沒有訪問修飾符,區塊外部無法使用的變量。
沒有默認值,所以必須賦初始值
生命周期即為方法的生命周期
Example:
參數(input parameter, parameter (variable), argument)
這個就不多說了,要注意的是 argument 和 parameter 的區別(下文)。
另外,Oracle 官方文檔中將參數分為了構造參數、方法參數和異常參數三部分。
Example:
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 關鍵詞修飾的變量。不可變量屬于成員變量。
成員(member)
A field or method of a class. Unless specified otherwise, a member is not static.
指的是類中非靜態的成員變量或方法。(用法同field)
屬性(property)
Characteristics of an object that users can set, such as the color of a window.
可以被用戶設置或獲取的對象特征即為屬性。
POJO 或 JavaBean 中的成員變量也稱作屬性(具有set、getter方法)。
最后,總結一下國內目前的慣用法(英文取其一,序號對應上文):
field -> 成員變量, instance variable / non-static field -> 實例變量/非靜態變量
class variable -> 靜態變量
local variable -> 本地變量
input parameter -> 參數
final variable -> 常量
member -> 成員(用法同field)
property -> 屬性返回搜狐,查看更多
責任編輯:
總結
以上是生活随笔為你收集整理的property field java_Java 中 field 和 variable 区别及相关术语解释的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java logfaction_Java
- 下一篇: python中color的用法_pyth