在java中设置静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18391231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting a static variable in java
提问by frazman
I am new to java hence probably a very noob question:
我是 Java 新手,因此可能是一个非常菜鸟的问题:
I have a class
我有一堂课
public class Foo{
private static String foo;
private String bar;
public Foo(String bar){
this.bar = bar;
}
}
Now before I instantiate any object for class Foo, I want to set that static variable foo. which will be used in the class.. How do i do this?
现在,在为类 Foo 实例化任何对象之前,我想设置该静态变量 foo。这将在课堂上使用.. 我该怎么做?
Also, please correct my understanding. value of foo will be same across all the objects, hence it does make sense to declare it as static ? right?
另外,请纠正我的理解。foo 的值在所有对象中都相同,因此将其声明为 static 确实有意义吗?对?
采纳答案by Daniel Kaplan
public class Foo{
private static String foo = "initial value";
private String bar;
public Foo(String bar){
this.bar = bar;
}
}
Since the value will be the same across all objects, static
is the right thing to use. If the value is not only static
but also never changing, then you should do this instead:
由于所有对象的值都相同,static
因此使用它是正确的。如果值不仅static
而且永远不会改变,那么你应该这样做:
public class Foo{
private static final String FOO = "initial value";
private String bar;
public Foo(String bar){
this.bar = bar;
}
}
Notice how the capitalization changed there? That's the java convention. "Constants" are NAMED_LIKE_THIS.
注意那里的大写是如何变化的?这就是java约定。“常量”是 NAMED_LIKE_THIS。
回答by Eugene
foo
will be shared among all instances ofFoo
- To initialize it:
foo
将在所有实例之间共享Foo
- 要初始化它:
Option A
选项 A
private static String foo = "static variable";
private static String foo = "static variable";
Option B
选项 B
private static String foo;
static {
foo = "static variable";
}
Option B is seldom used, mostly when there are some inter-dependencies between static variables or potential exceptions.
选项 B 很少使用,主要是当静态变量或潜在异常之间存在一些相互依赖时。
In either case, static init will happen when the class is loaded.
在任何一种情况下,静态初始化都会在类加载时发生。
回答by Luke
As stated by the other answers, you should set your initial value like so:
正如其他答案所述,您应该像这样设置初始值:
private static String foo = "initial value";
Additionally, if you want to access this variable from anywhere, you need to reference it in a static context, like so:
此外,如果你想从任何地方访问这个变量,你需要在静态上下文中引用它,如下所示:
Foo.foo
where Foo
is the class name, and foo
is the variable name.
其中Foo
是类名,foo
是变量名。
This is actually very useful in understanding the concept of static variables. Rather than referencing foo
as a member of some instanceof the Foo
class, you are referencing foo
as a member of the class itself. So, for all instances of Foo
, the value of foo
will be the same because it is owned by the classand not the instance.
这对于理解静态变量的概念其实非常有用。您不是foo
作为类的某个实例的成员进行引用,而是作为类本身的成员Foo
进行引用foo
。因此,对于 的所有实例Foo
, 的值foo
将相同,因为它属于类而不是实例。
Inside the Foo
class, you can get away with just calling foo
without qualifying it with a class name.
在Foo
类内部,您可以通过调用foo
而无需使用类名对其进行限定。