为什么 java 中的静态方法应该只接受其方法内的最终或非最终变量,而不是静态的?
例如我有以下方法:
public static void myfunc(int somethig)
{
int a=10;
final int b=20;
static int c=30; //gives Error why?
}
最佳答案
The question is: why not?
考虑一下:静态局部变量意味着什么?
我建议唯一的合理意思是:
public class Foo {
static int bar = 21;
public void foo() {
static int bar = 42; // static local
return bar;
}
}
等同于:
public class Foo {
static int bar = 21;
private static foo$bar = 42; // equivalent to static local
public void foo() {
return bar;
}
}
换句话说,(假设的)静态局部变量等同于可见性规则略有不同的常规静态属性。
Java 语言的设计者可能考虑到了这一点,并决定静态局部变量所增加的实际值(value)太少,因此不值得将它们包含在语言中。 (当然,这就是我投票的方式。)
关于java - 为什么java中的静态方法应该只接受其方法内的最终或非最终变量,而不是静态的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3717388/