在Java中,静态方法内部声明的变量本身是静态的吗? [英] In Java, are variables declared inside static methods themselves static?
问题描述
假设如下:
private static boolean A()
{
int parsedUntil = 0;
...
...
...
}
is parsedUntil被认为是一个静态变量?我注意到我不能在静态函数中声明它是静态的。
Is parsedUntil considered to be a static variable? I noticed that I can't declare it as static inside this static function.
后续问题:我读到静态变量只会被初始化一次。这是否意味着我第一次调用函数A()时该值将被设置为零,但每隔一次我调用A(),该行被省略?
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?
推荐答案
不,它不是静态变量。这是一个局部变量。方法中声明的任何变量都是局部变量。如果你想要一个静态变量,你必须在方法之外声明它:
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.
这篇关于在Java中,静态方法内部声明的变量本身是静态的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!