错误代码:
public class StringOutput {
String s = "One World One Dream";
public static void main(String args[]) {
System.out.println(s);
}
}
这段代码会报错:Cannot make a static reference to the non-static field s。
解决办法:将变量s用static修饰,即static String s = "One World One Dream"
改正代码:
public class StringOutput {
static String s = "One World One Dream";
public static void main(String args[]) {
System.out.println(s);
}
}
或者将变量s置入调用方法的同一作用域下:
public class StringOutput {
public static void main(String args[]) {
String s = "One World One Dream";
System.out.println(s);
}
}