Java出现No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosin

最近研究LDA源代码时,里面涉及到Comparable方法的使用。以前用过这个排序方法,现在想回顾一下。以下是程序,感觉没问题啊,结果报错了:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).

以下是代码程序:

package collection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    /**
     * 根据order对User排序
     */
    public  class Student implements Comparable<Student>{
        private String name;
        private Integer grade;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Integer getGrade() {
            return grade;
        }
        public void setGrade(Integer grade) {
            this.grade = grade;
        }
        public int compareTo(Student arg0) {
            return this.getGrade().compareTo(arg0.getGrade());
        }
    }
    public static void main(String[] args) {

        Student student1 = new Student();
        student1.setName("a");
        student1.setGrade(80);
        Student student2 = new Student();
        student2.setName("b");
        student2.setGrade(59);
        List<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        Collections.sort(list);
        for(Student u : list){
            System.out.println(u.getName());
        }

    }

}

于是百度谷歌了一下相关资料。原来我写的内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以在不做其他变动的情况下,最简单的解决办法是将public class改为public static class.

package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    /**
     * 根据order对User排序
     */
    public static class Student implements Comparable<Student>{
        private String name;
        private Integer grade;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public Integer getGrade() {
            return grade;
        }
        public void setGrade(Integer grade) {
            this.grade = grade;
        }
        public int compareTo(Student arg0) {
            return this.getGrade().compareTo(arg0.getGrade());
        }
    }
    public static void main(String[] args) {

        Student student1 = new Student();
        student1.setName("a");
        student1.setGrade(80);
        Student student2 = new Student();
        student2.setName("b");
        student2.setGrade(59);
        List<Student> list = new ArrayList<Student>();
        list.add(student1);
        list.add(student2);
        Collections.sort(list);
        for(Student u : list){
            System.out.println(u.getName());
        }

    }

}

再次运行,发现没问题了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值