听说java中静态方法只能使用静态变量。 但是,main 方法也是静态的,对吧?
最佳答案
您的问题:“静态方法应该只使用静态变量”这一说法是否正确?
没有。该说法不正确。
正确的说法是“静态方法只能使用那些定义为静态的实例变量”
看看下面的代码并阅读注释:
Class A{
int i;
static int j;
public static void methodA(){
i = 5; //cannot use since i is not static
j = 2; //can use.
int k = 3; //k is local variable and can be used no problem
**EDIT:**//if you want to access i
A a = new A();
//A.i = 5; //can use.
a.i = 5; // it should be non-capital "a" right?
}
}
关于java - java中的静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2392028/