publicclassA {privateint x; // 实例变量privatestaticint y; // 静态变量publicstaticvoidmain(String[] args) {// int x = A.x; // Non-static field 'x' cannot be referenced from a static contextA a =newA();int x =a.x;int y =A.y; }}
publicabstractclassA {publicstaticvoidfunc1(){ }// public abstract static void func2(); // Illegal combination of modifiers: 'abstract' and 'static'}
只能访问所属类的静态字段和静态方法,方法中不能有 this 和 super 关键字。
publicclassA {privatestaticint x;privateint y;publicstaticvoidfunc1(){int a = x;// int b = y; // Non-static field 'y' cannot be referenced from a static context// int b = this.y; // 'A.this' cannot be referenced from a static context }}