Java 无法在静态方法中声明静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24269308/
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
Unable to declare static variable inside of static method
提问by OldSchool
class Foo {
public Foo() { }
}
class Bar {
static Foo foo = new Foo(); // This is legal...
public static void main(String[] args) {
static int a = 0; // ... But why this is not?
}
}
Why can't we declare a static variable inside of a static function?
为什么我们不能在静态函数中声明一个静态变量?
回答by Tim B
Declare the variable as final, not as static.
将变量声明为最终变量,而不是静态变量。
Static means that there is one per class not one per instance of the class. Final means it can't be modified after creation. (Although note that making a reference final does not make the class it references immutable).
静态意味着每个类有一个,而不是类的每个实例一个。Final 表示创建后无法修改。(尽管请注意,使引用最终不会使其引用的类不可变)。
In other words if you have a
换句话说,如果你有一个
final String[] array = new String[3];
You can no longer change that variable, for example if you wanted to assign to it a new array with a different size you could not. However you can modify the contents of the array.
您不能再更改该变量,例如,如果您想为其分配一个大小不同的新数组,则无法更改。但是,您可以修改数组的内容。
array[0] = "test";
Because this modifies the contents, not the array itself.
因为这会修改内容,而不是数组本身。
The same thing holds for any mutable objects.
同样的事情也适用于任何可变对象。
回答by Tina J
You have to make the static final static
or remove static
.
您必须使 staticfinal static
或 remove static
。
In Java, static means that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects. This means that static keyword can be used only in a 'class scope'.
在 Java 中,静态意味着它是一个类的变量/方法,它属于整个类但不属于其某些对象之一。这意味着 static 关键字只能在“类范围”中使用。
Generally, in C, you can have statically allocated locally scoped variables. Unfortunately this is not directly supported in Java. But you can achieve the same effect by using nested classes.
通常,在 C 中,您可以静态分配局部范围的变量。不幸的是,这在 Java 中并不直接支持。但是您可以通过使用嵌套类来达到相同的效果。
For example, the following is allowed but it is bad engineering, because the scope of x is much larger than it needs to be. Also there is a non-obvious dependency between two members (x and getNextValue).
例如,下面是允许的,但它是糟糕的工程,因为 x 的范围比它需要的要大得多。此外,两个成员(x 和 getNextValue)之间也存在不明显的依赖关系。
static int x = 42;
public static int getNextValue() {
return ++x;
}
One would really like to do the following, but it is not legal:
人们真的很想做以下事情,但这是不合法的:
public static int getNextValue() {
static int x = 42; // not legal :-(
return ++x;
}
However you could do this instead,
但是你可以这样做,
public static class getNext {
static int x = 42;
public static int value() {
return ++x;
}
}
It is better engineering at the expense of some ugliness.
这是更好的工程,但要牺牲一些丑陋。
回答by DJClayworth
Other people have explained how to deal with this at the coding level. Allow me to explain the logical and philosophical reasons why static within a method makes no sense. You have to ask the question "How long do you want the variable to last?".
其他人已经解释了如何在编码级别处理这个问题。请允许我解释为什么方法中的静态没有意义的逻辑和哲学原因。您必须问“您希望变量持续多长时间?”的问题。
- normal member variables last as long as the instance they are part of;
- variables declared within a method last until the method is exited;
- static class variables last for the lifetime of the class (i.e. forever for most purposes)
- 普通成员变量的持续时间与它们所属的实例一样长;
- 在方法中声明的变量一直持续到方法退出;
- 静态类变量在类的生命周期内持续(即对于大多数用途来说是永远的)
So how long do you want your 'static within a method' variable to last? If it's until the end of the method, then you can just use it without static. If it's for the lifetime of the class, then you can declare it as a static member variable. What other options are there?
那么,您希望“方法内的静态”变量持续多长时间?如果一直到方法结束,那么您可以直接使用它而不使用静态。如果是在类的生命周期内,则可以将其声明为静态成员变量。还有哪些其他选择?
C++ allows statics within a method, but they end up behaving just like a static class variable, but with reduced scope. Even in C++ they are rarely used. They also end up being stored exactly like static member variables. The pattern is widely considered to be dangerous and confusing, because it mounts to having a method 'remember' the value of what looks like a local variable from one invocation to another - a value which would be changed if some other piece of code chooses to execute the method in between the two invocations.
C++ 允许在方法中使用静态,但它们最终的行为就像静态类变量一样,但范围缩小了。即使在 C++ 中,它们也很少使用。它们最终也完全像静态成员变量一样存储。该模式被广泛认为是危险和令人困惑的,因为它使方法“记住”从一次调用到另一次调用的局部变量的值 - 如果其他代码选择该值,该值将被更改在两次调用之间执行该方法。
The Java designers decided that the small amount of benefit wasn't worth the additional complexity of the language.
Java 设计者认为,少量的好处不值得语言的额外复杂性。