2016-11-01 137 views
-1

在Java中有許多關於staticfinal變量的討論。 我真的很想知道以下聲明之間的區別。似乎很混淆在java類中聲明一個變量(private,static,final)

public class foo() { 
    private static final int a; 
    private static int b; 
    private final int c; 
    private int d; 
    public static final int e; 
    public static int f; 
    public final int g; 
    public int h; 
} 

哪一個可以修改/訪問類內部/外部?

P.S:In Java, difference between default, public, protected, and private中的問題是一個較大的範圍。我的重點是一些令人困惑的問題!

+0

'private'表示它對這個類是私人的,並且不在那個類之外訪問。 –

回答

4

private表示只能由foo類的實例訪問它。

public表示可以從擁有對foo類實例的引用的任何對象訪問它。

static表示它屬於該類,因此它由所有foo實例共享。

final表示它不能更改其初始值。

final屬性在初始化後無法修改。 static屬性可以修改,但請記住新值由所有實例共享。 private屬性只能由foo實例本身修改。

這意味着一個static final屬性:不能被修改;由所有實例共享。

1

public屬性可以從任何類訪問。

private屬性可以在聲明的類中進行訪問。 (這就是爲什麼我們需要在其他類中包含getter和setter來檢索私人變量的原因)

final屬性無法修改並設置爲不同的值。

static屬性在類本身和其實例中被訪問。

+0

爲什麼這會倒下?1 –

-2
  1. private意味着它不能在課堂以外看到。
  2. static表示變量與類關聯,而不是對象。這意味着對於任何對象,其靜態變量將由同一類的所有對象共享。 (more information)
  3. final意味着一旦您爲變量賦值,就不能重新賦值。

從課外您只能訪問public變量,但不能修改final的變量。

1
private static final int a; // accessed only  /inside only 
private static  int b; // accessed and modified/inside only 
private  final int c; // accessed only  /inside only 
private    int d; // accessed and modified/inside only 
public static final int e; // accessed only  /inside and outside 
public static  int f; // accessed and modified/inside and outside 
public   final int g; // accessed only  /inside and outside 
public    int h; // accessed and modified/inside and outside 

正如你可以看到:

  • static這裏沒有任何作用
  • final減少accessed and modifiedaccessed only
  • private/public決定inside only/inside and outside
+0

你可能想要限定你的斷言「靜態在這裏沒有任何作用」,以澄清你的意思是說它對可訪問性沒有任何影響。它當然會以其他方式發揮作用。 –

+0

@KlitosKyriacou:是的,在發佈我的答案後,我確實想到了一些。然而,我認爲'here'這個詞暗示了它,除此之外(更有說服力) - 關鍵字沒有任何作用是沒有意義的,所以我認爲很明顯「沒有效果」是在問題的背景......你怎麼看? –

0

在類標識符的上下文中,語句具有以下解釋的含義: 首先privatepublic關鍵字是訪問符號,這意味着所有具有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 

乾杯!

0

public - 類,成員方法,構造器,接口等聲明爲public,可從其他class.we訪問可以說,它擁有全球訪問。

private -Member聲明爲private的方法,memeber變量和構造函數只能在聲明的類本身中訪問。如果類中存在公共getter方法,則可以在類外部訪問聲明爲private的變量。

final - 最終將確保該字段是一個常數,並且不能被改變

static - 它與類型和不與實例相關聯。即對於所有對象只有一個字段副本存在,而不是每個對象的單個副本。意味着單個副本的字段將在該類別的所有對象之間共享。

static final - 該類的所有實例將共享相同的值和第一初始化後不能被修改。