2012-02-22 92 views
6
public class Array                
{ 
    static String[] a = new String[] {"red", "green", "blue"}; 
    static Point[] p = new Point[] {new Point(1, 2), "3,4"}; 

    public static void main(String[] args) 
    { 
     System.out.println("hello"); 
    } 

    class Point 
    { 
     int x; 
     int y; 

     Point(int x, int y) 
     { 
      this.x = x; 
      this.y = y; 
     } 

     Point(String s) 
     { 
      String[] a = s.split(","); 
      x = a[0].parseInt(); 
      y = a[1].parseInt(); 
     } 
    } 
} 

在上述程序中,靜態Point陣列初始化失敗,報告錯誤:靜態對象陣列

Array.java:4: non-static variable this cannot be referenced from a static context 
    static Point[] p = new Point[] {new Point(1, 2), "3,4"}; 

但是,靜態String陣列成功。他們之間有什麼區別?

我真的需要一個靜態對象數組,因爲它很容易引用而不需要實例化外部類。

感謝

回答

4

你需要做Point靜態嵌套類,像這樣:

static class Point { 
1

Point[]必須是數組或Point"3,4"不是Point 實例,使Point類的靜態的此錯誤

1

以下是來自Oracle Online Reference的說明

Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass { 
    ... 
    class InnerClass { 
     ... 
    } 
} 

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

此外,它不能從封閉類型的靜態成員內訪問,因爲它需要一個實例存在。

1

聲明你Point類爲靜態:

... 
static class Point 
{ 
    ... 
} 

這是必要的,因爲Point是一個內部類(String不是)。

但是,{new Point(1, 2), "3,4"};是您的下一個問題。我想它應該是{new Point(1, 2), Point(3, 4)};

+0

謝謝。我想要「3,4」觸發點(字符串)。看來它不能這樣做。 – pengguang001 2012-02-22 08:55:16

+0

'Point(String xy){String [] tmp = xy.split(「,」); x = Integer.parseInt(tmp [0]); y = Integer.parseInt(tmp [1]); }'? – khachik 2012-02-22 09:59:41

0

你可能想做點靜態嵌套類。

static class Point { 

} 

檢查this

5

你要做三件事情,使你的代碼工作。我會解釋他們。首先看到工作版本。

public class Array { 
    static String[] a = new String[]{"red", "green", "blue"}; 
    static Point[] p = new Point[]{new Point(1, 2), new Point("3,4")}; 

    public static void main(String[] args) { 
     System.out.println("hello"); 
    } 

    static class Point { 
     int x; 
     int y; 

     Point(int x, int y) { 
      this.x = x; 
      this.y = y; 
     } 

     Point(String s) { 
      String[] a = s.split(","); 
      x = Integer.parseInt(a[0]); 
      y = Integer.parseInt(a[1]); 
     } 
    } 
} 

以下是您必須進行的三項更改。

1.改變"3,4"new Point("3,4")new Point(3,4)

我們知道,一個數組可以保存相似類型的項目。在此,您正在聲明類型爲Point的名爲p的數組。這意味着它只能包含Point類型的項目(或其子類型)。但第二個元素"3,4"的類型爲String,並且您有不匹配。因此,您必須指定new Point("3,4")new Point(3,4)以獲得類型爲Point的項目。

2.你需要讓你的Pointstatic

Java Tutorial

An instance of InnerClass can exist only within an instance of OuterClass 
and has direct access to the methods and fields of its enclosing instance. 

這裏您Point類是一個內部類,它必須給的Array所有成員訪問類。爲此,Point類的每個對象都必須關聯到Array類的對象。但是,您正在創建的數組pstatic上下文中。因此,您必須將Point級別設爲static級別,或將p設爲非靜態級別。

3. parseInt不是String類的方法

parseIntInteger是類的一個靜態方法不是String類和。所以你必須把它叫做Integer.parseInt(stringValue)

希望這會有所幫助:)

+0

感謝您的友好提醒。現在我的程序運行。 – pengguang001 2012-02-22 09:22:04