public class T {
    static int $i;
}
    public static void myfun() {
          this.i;
    }
方法myfun中,除了直接写i,还可以怎么写?this.i;不行,不能self::i。class

解决方案 »

  1.   


    public class T {
        static int i;    public static void myfun() {
              this.i = 9;
        }
    }
      

  2.   

    public class T {
        static int i;
     
        public static void myfun() {
              i = 9;
        }
    }静态变量不能用THIS
      

  3.   

    第一个问题:myfun方法写在类的外面。
    第二个问题:this.i语法错误,不构成一个语言。
    第三个问题:不能在静态方法中使用this和super关键字。第三个问题的解释:this关键字是对当前对象的引用,super是对父类的引用,静态成员优先于类加载到内存中,静态成员随着类的加载而加载,当静态成员调用this的时候,对象还没创建,自然就不能使用this了,super也是一样的。
      

  4.   


    public class T {
        static int $i;
    }
        public static void myfun() {
              T.i;
        }
      

  5.   


    public class Test3 {
        public static int $i=5;
        public int k;
        public Test3(int k) {
         this.k=k;
         Test3.$i=k;
        }
        public static void main(String[] args) {
    Test3 test1 = new Test3(10);
    Test3 test2 = new Test3(5);
    System.out.println(test1.k+"   "+test1.$i);
    System.out.println(test2.k+"   "+test2.$i);
    }
    }结果:
    10   5
    5   5
    这就是static和非static的区别