0

我正在用 Java 编程,我遇到了以下异常......

"No enclosing instance of type Host is accessible. Must qualify the allocation with an enclosing instance of type Host (e.g. x.new A() where x is an instance of Host)." 

这是相关的代码。任何人都告诉是什么导致了这个异常?

//Creating john     
    Employee john =new Employee(Name,Address,Age,Salary);
    //closing the scanner
    in.close();

    john.info();
}
class Employee
{
    //variables
    private String name ="";
    private String address="";
    private double salary=0.0;
    private int age=0;

    //constructor
    public Employee(String n, String add,int a, double s )
    {
        name = n;
        address = add;
        salary = s;
        age = a;        
    }
    public void info()
    {
        System.out.println(name);
        System.out.println(address);
        System.out.println(age);
        System.out.println(salary);
    }
4

1 回答 1

0

这个错误几乎解释了这一切。您可能有一个非静态内部类,Host并且您试图在静态上下文(即Host.Inner obj = new Host.Inner())中实例化它。您可以将该类设为静态或使用外部类的实例对其进行实例化。所有这些信息都可以在docs中找到。一个简单的谷歌搜索就会发现这一点。

于 2013-08-02T19:28:48.687 回答