2013-08-02 33 views
0

我編程在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); 
    } 
+0

請務必爲相關編程語言添加標籤。這樣,那些瞭解這種語言的人(Java?)就能夠找到你的問題。 – BergQuester

+3

另外,我知道我們很多人會發布太多的代碼,但同時,如果你忽略了重要的代碼(這裏的主機代碼是'Host'?),我們根本無法提供幫助。 –

+0

可能的[Java的重複 - 沒有可以訪問Foo類型的封閉實例](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian

回答

0

這個錯誤幾乎可以解釋這一切。您可能在Host中有一個非靜態內部類,並且您試圖在靜態上下文中對其進行實例化(即Host.Inner obj = new Host.Inner())。您可以將該類設爲靜態,也可以使用外部類的實例對其進行實例化。所有這些信息均可在docs中找到。一個簡單的谷歌搜索會顯示。