在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:09:14  来源:igfitidea点击:

setting a static variable in java

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, staticis the right thing to use. If the value is not only staticbut 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

  1. foowill be shared among all instances of Foo
  2. To initialize it:
  1. foo将在所有实例之间共享 Foo
  2. 要初始化它:

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 Foois the class name, and foois the variable name.

其中Foo是类名,foo是变量名。

This is actually very useful in understanding the concept of static variables. Rather than referencing fooas a member of some instanceof the Fooclass, you are referencing fooas a member of the class itself. So, for all instances of Foo, the value of foowill be the same because it is owned by the classand not the instance.

这对于理解静态变量的概念其实非常有用。您不是foo作为类的某个实例的成员进行引用,而是作为类本身的成员Foo进行引用foo。因此,对于 的所有实例Foo, 的值foo将相同,因为它属于而不是实例

Inside the Fooclass, you can get away with just calling foowithout qualifying it with a class name.

Foo类内部,您可以通过调用foo而无需使用类名对其进行限定。