java - Java类错误

我正在用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);
    }

最佳答案

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

https://stackoverflow.com/questions/18022694/

相关文章:

c++ - 复制构造函数 - 编译器错误 C2040 和 C2440

java - 需要一个实例化类的方法被调用

python - Python反转计数器: slice indicies error

compiler-errors - 错误在Centos6.4中更新lib.so.6文件

vba - Excel VBA : Compiler Errors

c++ - KEIL错误: no instance of overloaded function “

c++ - Googletest 编译错误 : ‘xyzTest’ was not declared

android - 使用二进制文字数字时出错。为什么要使用旧的JDK?

wpf - 当应用程序类型为类库时,未定义的CLR namespace

java - 无法解析为类型-在RHEL 5上编译servlet(tomcat)时出错