编辑:是否有可能或有用的初始化类外的变量? 例如,如果我有这样的代码:是否可以在类之外初始化变量?
static int example = 1;
public class test {
public static void main(String []args) {
System.out.println("I have " + example + " question!");
}
还是更多有用做到这一点就像这样:
public class test {
static int example = 1;
public static void main(String []args) {
System.out.println("I have " + example + " question!");
}
如果这两种方式都是可能的,有什么区别? 感谢您提前
你的第一个例子不会编译。 –
你试过了吗? – shmosel
这两个示例都不会编译 – baao