在類標識符的上下文中,語句具有以下解釋的含義: 首先private
和public
關鍵字是訪問符號,這意味着所有具有private
關鍵字的成員僅在它們聲明的類的範圍內可見。所有public
關鍵字的成員是類的範圍之外可見。
如
class foo{
public int myVar;
private int myBar;
}
現在,如果你實例化'富」
foo f = new foo();
f.myVar // is valid
f.myBar // is invalid and generates compile time error.
static
關鍵字表示該標識符的分配是類的所有實例常見,所以這個標識的,在分配編譯器第一次遇到類型定義的時間。因爲,對於任何類類型分配只發生一次,只有一個保持靜態字段,它可以跨越這個類的所有對象進行訪問的實例。
final
關鍵字表示(在標識符的上下文中),這些標識符可被初始化一次,然後被關閉任何修改。 (在方法方面它意味着該方法不能由派生類中重寫)。
現在回到你的問題,下面的語句將意味着:
private static final int a;
// 'a' has scope local to the class and its value is accessible through Type
// and shared across all instances of that type and its value cannot be
// changed once initialised.
private static int b;
// 'b' has scope local to the class and its value is accessible through Type
// and shared across all instances of that type.
private final int c;
// 'c' has scope local to the class and its value cannot be changed once
// initialised.
private int d;
// 'd' has scope local to the class
public static final int e;
// 'e' has scope beyond the class, it can be accessed outside the class
// through an instance of this class and its value is accessible through
// Type and shared across all instances of that type
// and its value cannot be changed once initialised.
public static int f;
// 'f' has scope beyond the class, it can be accessed outside the class and
// value is accessible through Type and shared across all instances of that
// type
public final int g;
// 'g' has scope beyond the class, it can be accessed outside the class
// through an instance of this class
// and its value cannot be changed once initialised.
public int h;
// 'h' has scope beyond the class, it can be accessed outside the class
// through an instance of this class
乾杯!
'private'表示它對這個類是私人的,並且不在那個類之外訪問。 –