【发布时间】:2012-11-10 23:14:32
【问题描述】:
所以在我的小 Java 程序中,我在 main 方法中声明了所有变量,但我希望能够从类中的任何位置修改它们,所以我改为在类主体中声明它们。现在我不能再在 main 方法中修改它们了。将变量声明为静态的解决方案是什么?如果我这样做,我仍然可以从 main 方法中更改这些变量吗?
public class MainGUI
{
int num1= 1366, num2= 528, num3= 482, sum; // declare these static?
public static void main(String args[])
{
sum = num1 + num2+ num3; //compiler tells me "non-static varable cannot be refrenced from a static context"
}
}
【问题讨论】:
-
在类中定义变量。