在 Java 中,在静态方法中声明的变量本身是静态的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6426117/
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
In Java, are variables declared inside static methods themselves static?
提问by Thomas Johansson
Assume the following:
假设如下:
private static boolean A() { int parsedUntil = 0; ... ... ... }
Is parsedUntil considered to be a static variable? I noticed that I can't declare it as static inside this static function.
parsedUntil 是否被认为是一个静态变量?我注意到我不能在这个静态函数中将它声明为静态。
Follow-up question: I read that a static variable will only be initialized once. Does that mean the first time I call function A() the value will be set to zero, but every other time I call A(), that row is omitted?
后续问题:我读到静态变量只会被初始化一次。这是否意味着我第一次调用函数 A() 时,该值将设置为零,但每隔一次调用 A() 时,该行就会被省略?
采纳答案by Jon Skeet
No, it's not a static variable. It's a local variable. Any variable declared in a method is a local variable. If you want a static variable, you have to declare it outside the method:
不,它不是静态变量。这是一个局部变量。在方法中声明的任何变量都是局部变量。如果你想要一个静态变量,你必须在方法之外声明它:
private static int parsedUntil = 0;
There's no way of declaring a static variable which can only be used within a single method.
无法声明只能在单个方法中使用的静态变量。
回答by Sean Patrick Floyd
no, A()
is a static method, and parsedUntil
is a local variable inside A.
不,A()
是静态方法,parsedUntil
是 A 内部的局部变量。
Modifiers like static
are not valid in local variables (only final
is permitted afaik)
修饰符static
在局部变量中无效(只final
允许 afaik)
Follow-up question: I read that a static variable will only be initialized once.
后续问题:我读到静态变量只会被初始化一次。
true
真的
Does that mean the first time I call function A() the value will be set to zero, but every other time I call A(), that row is omitted?
这是否意味着我第一次调用函数 A() 时,该值将设置为零,但每隔一次调用 A() 时,该行就会被省略?
since parsedUntil is not a static field, but a local variable in a static method, this is not the case.
由于 parsedUntil 不是静态字段,而是静态方法中的局部变量,因此情况并非如此。
回答by Blagovest Buyukliev
static
variables cannot be declared locally inside methods - they can only be members of a class, and they get initialised when the class is loaded.
static
变量不能在方法内部局部声明——它们只能是类的成员,并且在加载类时初始化。
回答by Oliver Charlesworth
Java does not have static local variables like C or C++ does, so you can never have static int parsedUtil = 0;
.
Java 没有像 C 或 C++ 那样的静态局部变量,所以你永远不可能有static int parsedUtil = 0;
.
So no, parsedUtil
is not in any sense "static". Its value is initialised to 0 every time the method is executed.
所以不,parsedUtil
在任何意义上都不是“静态的”。每次执行该方法时,它的值都会初始化为 0。
回答by Ievgen Savenko
No it's not C.
不,这不是 C。
parsedUntil is not static. It's just a local variable. You cannot declare static variable inside the method.
parsedUntil 不是静态的。它只是一个局部变量。您不能在方法内声明静态变量。
Regarding second question - static variables can be assigned as many times as you want. You cannot reassign only final variables.
关于第二个问题 - 可以根据需要多次分配静态变量。您不能只重新分配最终变量。