2016-11-01 140 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 - 该类的所有实例将共享相同的值和第一初始化后不能被修改。