2014-02-11 158 views
-1

我正在嘗試使用JOptionPane顯示我的人員對象(名稱,年齡)。 (名稱和年齡是命令行參數中的字符串和整數)。但是,我收到以下錯誤:從類錯誤創建新對象Java

BasicClass.java:9: error: non-static variable this cannot be referenced from a static context 
     Person person1 = new Person(args[0], age1); 
        ^

BasicClass.java:10: error: non-static variable this cannot be referenced from a static context 
     Person person2 = new Person(args[2], age2); 
        ^

我是否正確初始化第一個Person對象到命令行輸入?

EDIT 對不起,我忘記了自己的代碼示例:

import javax.swing.JOptionPane; 

public class BasicClass{ 
    public static void main(String[] args){ 
     Integer age1 = Integer.parseInt(args[1]); 
     Integer age2 = Integer.parseInt(args[3]); 

    // Create two Person Objects 
     Person person1 = new Person(args[0], age1); 
     Person person2 = new Person(args[2], age2); 

    // toString() method to display first & second Person Object 
     String firstOutput = person1.toString(); 
     String secondOutput = person2.toString(); 
     JOptionPane.showMessageDialog(null, firstOutput); 
     JOptionPane.showMessageDialog(null, secondOutput); 
    } 

    // Object 
    class Person{ 
    // Data fields that will store each of the objects data 
     private String name; 
     private Integer age; 

    // Constructor 
     public Person(String n1, int a1){ 
     name = n1; 
     age = a1; 
     } 

     public String toString(){ 
     String output = name + " is" + age + " years old."; 
     return output; 
     } 
    } 
} 
+1

在此處張貼您的代碼 –

+0

如果沒有查看您的代碼,無法發表評論。發佈Person類和發生此錯誤的類。 – RaviH

+1

讓Person類靜態 –

回答

2

Person是非靜態的內部類,這意味着它是構件外類(BasicClass)的實例的沒有這個實例你不能使用它(就像方法或領域一樣)。

既然你是在靜態方法和靜態方法使用這個類沒有默認this引用考慮

  • 使Personstatic
  • 或創建外部類的實例通過它您將創建Person類的實例,如

    BasicClass basic = new BasicClass(); 
    Person p = basic.new Person(); 
    
  • 或將您的代碼Person課外BasicClass班。

+0

感謝您的詳細解釋Pshemo!我其實打算關閉我的BasicClass ...但是我沒有我的右括號。真是太遺憾了,因爲一個簡單的錯誤,所有這些麻煩:(雖然,我仍然感謝你的解釋,因爲它給了我更多的理解類 –

+1

@ Asiax3歡迎:) – Pshemo

0

這是因爲您在靜態上下文中調用非靜態類。一種方法是在主類中創建Person對象,然後創建主類對象。否則,您應該將Person類聲明爲靜態類,如

import javax.swing.JOptionPane; 

public class Main{ 
    public static void main(String[] args){ 
     Integer age1 = Integer.parseInt(args[1]); 
     Integer age2 = Integer.parseInt(args[3]); 

    // Create two Person Objects 
     Person person1 = new Person(args[0], age1); 
     Person person2 = new Person(args[2], age2); 

    // toString() method to display first & second Person Object 
     String firstOutput = person1.toString(); 
     String secondOutput = person2.toString(); 
     JOptionPane.showMessageDialog(null, firstOutput); 
     JOptionPane.showMessageDialog(null, secondOutput); 
    } 

    // Object 
    static class Person{ 
    // Data fields that will store each of the objects data 
     private String name; 
     private Integer age; 

    // Constructor 
     public Person(String n1, int a1){ 
     name = n1; 
     age = a1; 
     } 

     public String toString(){ 
     String output = name + " is" + age + " years old."; 
     return output; 
     } 
    } 
} 

請參閱another example