- public class Test {
- private static int n = 0;
- public static void setNum(int n) {
- this.n = n; //Cannot use this in a static context
- }
- public static void main(String[] args) {
- setNum(0);
- }
- }
意思是:this关键字不能在static静态方法中使用
因为:
Static方法是类方法,先于任何的实例(对象)存在。即Static方法在类加载时就已经存在了,但是对象是在创建时才在内存中生成。而this指代的是当前的对象