2013-09-27 43 views
0

下面是一些命令的println缺少在main方法的代碼本身我的ArrayList不會承認什麼,我添加到列表中

import java.util.ArrayList; 

public class Student { 
    private String name; 
    private int age; 

    public Student (String n, int a) { 
     name = n; 
     age = a; 

    } 

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

    ArrayList<Student> rayList = new ArrayList<Student>(); 
    rayList.add(new Student("Sam", 17)); 
    rayList.add(new Student("Sandra", 18)); 
    rayList.add(new Student("Billy", 16)); 
    rayList.add(new Student("Greg", 17)); 
    rayList.add(new Student("Jill", 18)); 

    public static void main(String[] args) { 
     System.out.println(rayList.get(0)); 
    } 

} 

。 但是,當我嘗試了5名學生加入到我的ArrayList中,我得到錯誤「不能讓一個靜態引用非靜態字段rayList」

回答

1

這是因爲您正在訪問non static fieldstatic function。 就你的情況而言。

ArrayList<Student> rayList = new ArrayList<Student>(); // declare instance variable. 

而你不能訪問實例變量而不創建類的實例。

而是使用,

public static void main(String[] args) { 
    ArrayList<Student> rayList = new ArrayList<Student>(); // creates local instance of rayList 
    rayList.add(new Student("Sam", 17)); 
    rayList.add(new Student("Sandra", 18)); 
    rayList.add(new Student("Billy", 16)); 
    rayList.add(new Student("Greg", 17)); 
    rayList.add(new Student("Jill", 18)); 

    System.out.println(rayList.get(0)); 
} 
6

你想可執行上下文之外執行代碼。代碼只能從方法,靜態初始化程序或實例初始化程序(感謝NickC)上下文中執行。

嘗試將其移動到main方法入手...

public static void main(String[] args) { 
    ArrayList<Student> rayList = new ArrayList<Student>(); 
    rayList.add(new Student("Sam", 17)); 
    rayList.add(new Student("Sandra", 18)); 
    rayList.add(new Student("Billy", 16)); 
    rayList.add(new Student("Greg", 17)); 
    rayList.add(new Student("Jill", 18)); 
    System.out.println(rayList.get(0)); 
} 

更新基於反饋

生成你的第一個錯誤Cannot make a static reference to the non-static field rayList因爲rayList未聲明static,但你試圖從static上下文中引用它。

// Not static 
ArrayList<Student> rayList = new ArrayList<Student>(); 
// Is static 
public static void main(String[] args) { 
    // Can not be resolved. 
    System.out.println(rayList.get(0)); 
} 

rayList被聲明爲「實例」字段/變量,這意味着它需要聲明類(Student)它具有意義之前的一個實例。

這可以通過...

main創建的Student一個實例,並通過該實例訪問它,例如個人來解決......

public static void main(String[] args) { 
    Student student = new Student(...); 
    //... 
    System.out.println(student.rayList.get(0)); 
} 

,我不喜歡這樣的,rayList並不真正屬於Student,它沒有增加任何價值。您可以想象在將任何內容添加到List之前,必須創建一個Student的實例嗎?

我也不喜歡直接訪問實例字段,但這是個人偏好。

製作rayListstatic

static ArrayList<Student> rayList = new ArrayList<Student>(); 
// Is static 
public static void main(String[] args) { 
    //... 
    System.out.println(rayList.get(0)); 
} 

這是一個可行的選擇,但需要更多的情況下,才能夠視爲好還是壞。我個人認爲static領域可能會導致比他們解決更多的問題,但同樣,這是個人觀點,您的背景可能會認爲是合理的解決方案。

或者,您可以在static方法的上下文中創建List的本地實例,如第一個示例中所示。

public static void main(String[] args) { 
    ArrayList<Student> rayList = new ArrayList<Student>(); 
    //... 
    System.out.println(rayList.get(0)); 
} 

正如我所說,你選擇做哪一個將會歸結你,我個人更喜歡最後兩個,但那就是我。

+0

你已經解決了這個問題(有2個),但只解釋了爲什麼它被破壞的一部分,而不是OP詢問的部分。不,靜態初始化器和方法不是可以執行代碼的唯一上下文。也有實例初始化器。 – Nicole

+0

@NickC第一感謝實例初始化者,第二,你能解釋「而不是OP詢問的問題」嗎?正如我所看到的,OP不知道爲什麼他們的代碼出錯,這是因爲執行上下文...... – MadProgrammer

+2

在我看來,編譯器發現的第一個錯誤是'rayList'不是靜態的但是從靜態方法'main'引用。我認爲OP也應該知道這一點,但重要的是你也發現了另一個錯誤。 – Nicole

0

充分利用rayList靜態變量使用它在main方法是這樣的:

然後在主方法中添加對象,並使用它:

static ArrayList<Student> rayList = new ArrayList<Student>(); 
public static void main(String[] arg){ 

    rayList.add(new Student("Sam", 17)); 
    rayList.add(new Student("Sandra", 18)); 
    rayList.add(new Student("Billy", 16)); 
    rayList.add(new Student("Greg", 17)); 
    rayList.add(new Student("Jill", 18)); 

    //Do whatever you want. 
} 
1

該錯誤意味着rayList不是靜態的(在換句話說,它是類Student)的成員,但你main方法是:

/** THIS IS NOT STATIC **/ 
ArrayList<Student> rayList = new ArrayList<Student>(); 
... 

public static void main(String[] args) { 
    /** THIS SCOPE IS STATIC **/ 
    System.out.println(rayList.get(0)); 
} 

實際上,您不能將Student s列爲Student的成員,因爲您將因堆棧溢出或內存不足而耗盡內存,這取決於您是如何創建它的,所以我猜你希望rayList是靜態的。這將使rayList不再是Student的成員。

您也不能將您在初始化程序塊或方法之外的方式添加到您的ArrayList

在這裏看到更多的初始化語句塊:

或者你可以移動的一切參照rayListmain方法內。

相關問題