在 java class 中声明一个变量(私有、静态、最终)
declaringn a variable in java class (private,static,final)
Java中有很多关于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;
}
哪个可以是modified/accessedinside/outside的class?
P.S:In Java, difference between default, public, protected, and private中的问题范围更大。我的重点是一些令人困惑的点!
private
表示它只能被 class foo
.
的实例访问
public
意味着它可以从拥有对 Class foo
.
实例的引用的任何对象访问
static
表示它属于 class,因此,它被所有 foo
个实例共享。
final
表示不能改变初始值
final
属性一旦初始化就无法修改。 static
属性可以修改,但请记住新值由所有实例共享。 private
属性只能由 foo
实例本身修改。
这意味着一个static final
属性:不能被修改;由所有实例共享。
public
属性可以从任何 class.
访问
private
属性只能在声明它的 class 中访问。 (这就是为什么我们需要在其他 classes 中包含 getter 和 setter 来检索私有变量)
final
属性无法修改和设置为不同的值。
static
属性在 class 本身及其实例中被访问。
private
表示在class之外是看不到的。
static
表示变量与 class 关联,而不是对象。这意味着对于任何对象,其静态变量将被同一class的所有对象共享。 (more information)
final
表示一旦给变量赋值,就不能重新赋值。
从 class 外部您只能访问 public
变量,但不能修改 final
变量。
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 modified
减少为 accessed only
private
/public
确定 inside only
/ inside and outside
在 class 标识符的上下文中,语句的含义如下所述:
首先 private
和 public
关键字是访问指示符,这意味着所有带有 private
关键字的成员仅在它们声明的 class 范围内可见。所有带有 public
关键字的成员在 class.
范围之外可见
例如
class foo{
public int myVar;
private int myBar;
}
现在如果你实例化 `foo'
foo f = new foo();
f.myVar // is valid
f.myBar // is invalid and generates compile time error.
static
关键字表示标识符的分配对于class的所有实例是通用的,所以这个标识符是在编译器第一次遇到类型定义时分配的。因为,任何 class 的类型分配只发生一次,所以只维护一个静态字段实例,可以跨此 class.
的所有对象访问。
final
关键字表示(在标识符的上下文中),这些标识符可以初始化一次,然后关闭以进行任何修改。
(在方法的上下文中,这意味着该方法不能被派生的 classes 覆盖)。
现在回到你的问题,以下陈述将意味着:
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
干杯!
public
- class、成员方法、构造函数、接口等声明的 public 可以从任何其他 class.we 访问,可以说它具有全局访问权限。
private
-声明为私有的成员方法、成员变量和构造函数只能在声明的class内访问itself.Variables声明为私有的可以在[=外访问28=] 如果 public getter 方法存在于 class.
final
- final 会保证字段是常量,不能改变
static
-它与类型相关联,而不与实例相关联。即,所有对象将只存在该字段的一个副本,而不是每个对象的单独副本 object.means 字段的单个副本将在该 class
的所有对象之间共享
static final
- class 的所有实例将共享相同的值,并且在首次初始化后无法修改。
Java中有很多关于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;
}
哪个可以是modified/accessedinside/outside的class?
P.S:In Java, difference between default, public, protected, and private中的问题范围更大。我的重点是一些令人困惑的点!
private
表示它只能被 class foo
.
public
意味着它可以从拥有对 Class foo
.
static
表示它属于 class,因此,它被所有 foo
个实例共享。
final
表示不能改变初始值
final
属性一旦初始化就无法修改。 static
属性可以修改,但请记住新值由所有实例共享。 private
属性只能由 foo
实例本身修改。
这意味着一个static final
属性:不能被修改;由所有实例共享。
public
属性可以从任何 class.
private
属性只能在声明它的 class 中访问。 (这就是为什么我们需要在其他 classes 中包含 getter 和 setter 来检索私有变量)
final
属性无法修改和设置为不同的值。
static
属性在 class 本身及其实例中被访问。
private
表示在class之外是看不到的。static
表示变量与 class 关联,而不是对象。这意味着对于任何对象,其静态变量将被同一class的所有对象共享。 (more information)final
表示一旦给变量赋值,就不能重新赋值。
从 class 外部您只能访问 public
变量,但不能修改 final
变量。
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 modified
减少为accessed only
private
/public
确定inside only
/inside and outside
在 class 标识符的上下文中,语句的含义如下所述:
首先 private
和 public
关键字是访问指示符,这意味着所有带有 private
关键字的成员仅在它们声明的 class 范围内可见。所有带有 public
关键字的成员在 class.
例如
class foo{
public int myVar;
private int myBar;
}
现在如果你实例化 `foo'
foo f = new foo();
f.myVar // is valid
f.myBar // is invalid and generates compile time error.
static
关键字表示标识符的分配对于class的所有实例是通用的,所以这个标识符是在编译器第一次遇到类型定义时分配的。因为,任何 class 的类型分配只发生一次,所以只维护一个静态字段实例,可以跨此 class.
final
关键字表示(在标识符的上下文中),这些标识符可以初始化一次,然后关闭以进行任何修改。
(在方法的上下文中,这意味着该方法不能被派生的 classes 覆盖)。
现在回到你的问题,以下陈述将意味着:
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
干杯!
public
- class、成员方法、构造函数、接口等声明的 public 可以从任何其他 class.we 访问,可以说它具有全局访问权限。
private
-声明为私有的成员方法、成员变量和构造函数只能在声明的class内访问itself.Variables声明为私有的可以在[=外访问28=] 如果 public getter 方法存在于 class.
final
- final 会保证字段是常量,不能改变
static
-它与类型相关联,而不与实例相关联。即,所有对象将只存在该字段的一个副本,而不是每个对象的单独副本 object.means 字段的单个副本将在该 class
static final
- class 的所有实例将共享相同的值,并且在首次初始化后无法修改。