Java 如何在 Main 方法中声明静态变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3378758/
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
How do I declare a static variable inside the Main method?
提问by Priti
Can we declare Static
Variables inside Main
method? Because I am getting an error message:
我们可以Static
在Main
方法中声明变量吗?因为我收到一条错误消息:
Illegal Start of Expression
回答by Roman
Obviously, no, we can't.
显然,不,我们不能。
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.
在 Java 中,static
意味着它是一个类的变量/方法,它属于整个类但不属于其某些对象之一。
This means that static
keyword can be used only in a 'class scope' i.e. it doesn't have any sense inside methods.
这意味着static
关键字只能在“类范围”中使用,即它在方法内部没有任何意义。
回答by Karel Petranek
You cannot, why would you want to do that? You can always declare it on the class level where it belongs.
你不能,你为什么要这样做?您始终可以在它所属的类级别上声明它。
回答by MatrixFrog
You can use static variables inside your main
method (or any other method), but you need to declare them in the class:
您可以在main
方法(或任何其他方法)中使用静态变量,但需要在类中声明它们:
This is totally fine:
这完全没问题:
public Class YourClass {
static int someNumber = 5;
public static void main(String[] args) {
System.out.println(someNumber);
}
}
This is fine too, but in this case, someNumber
is a local variable, not a static one.
这也很好,但在这种情况下,它someNumber
是一个局部变量,而不是静态变量。
public Class YourClass {
public static void main(String[] args) {
int someNumber = 5;
System.out.println(someNumber);
}
}
回答by Shekhar
Because the static variables are allocated memory at the class loading time,and the memory is allocated only once.Now if you have a static variable inside a method,then that variable comes under the method's scope,not class's scope,and JVM is unable to allocate memory to it,because a method is called by the help of class's object,and that is at runtime,not at class loading time.
因为静态变量是在类加载时分配内存的,而且内存只分配一次。现在如果你在一个方法中有一个静态变量,那么这个变量就在方法的范围内,而不是类的范围内,JVM无法为它分配内存,因为一个方法是在类对象的帮助下调用的,而且是在运行时,而不是在类加载时。
回答by vivek
As static variables are available for the entire class so conceptually it can only be declared after the class whose scope is global where as static block or methods all have their own scope.
由于静态变量可用于整个类,因此从概念上讲,它只能在作用域为全局的类之后声明,而静态块或方法都有自己的作用域。
回答by John Henckel
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.
这是更好的工程,但要牺牲一些丑陋。