Java学习之路(一)--Thinking in Java

最近在进行《Tingking in Java》这本书的学习,第二章练习题目中练习一遇到了问题,No enclosing instance of type test1 is accessible. Must qualify the allocation with an enclosing instance of type test1 (e.g. x.new A() where x is an instance of test1).

       出错的原因是因为main方法是static静态方法,无法访问自己定义的动态类,即无法创建类的引用。改正方法很简单,可以直接在内部类前面添加static使之成为静态类。

      ​源代码如下:

public class test1 {

    public static class test{

    int s;

    char c;

}

public static void main(String[] args){

    test t = new test();

    System.out.println("s"+t.s);

    System.out.println("c"+t.c);

    }

}

  

posted @ 2015-09-20 23:16  VF_Ever  阅读(172)  评论(0编辑  收藏  举报