在静态环境中引用非静态数组列表?

时间:2019-02-19 11:47:04

标签: java arraylist static

我大约一个月前才开始学习Java,现在遇到“无法从静态上下文引用非静态变量studentList”的问题。我正在尝试使用一种与main不同的方法来填充学生列表,而不是为每个学生复制addStudent中的粘贴内容;但是我无法获取写入ArrayList的方法。 (错误:(14,27)java:无法从静态上下文引用非静态变量StudentList)。我知道数组不是静态的,因为它具有不确定的大小,但是如何使它按原样工作呢?有没有更好的办法?我可以将数组作为main方法的一部分,然后将其传递给addStudent,如果是的话,怎么办?

import java.util.ArrayList;

public class Main {
    ArrayList<Student> studentList = new ArrayList<>();

    public static void main(String []args) {
        addStudent("Adam", "Goldsmith", 70, 50);
        addStudent("John", "Smith", 20, 40);
        addStudent("Lewis", "Peterson", 90, 85);

        for (Student obj: studentList){
            System.out.println("Name: " + obj.studentForename + " "+ obj.studentSurname);
        }
    }

    public static void addStudent(String forename, String surname, int coursework, int test) {
        Student newStudent = new Student(forename, surname);
        newStudent.setForename(forename);
        newStudent.setSurname(surname);
        newStudent.averageMark(70, 65);

        studentList.add(newStudent);
    }

}

和我的“学生”班级:

public class Student {
    String studentForename;
    String studentSurname;

    public Student(String studentForename, String studentSurname) {
        setForename(studentForename);
        setSurname(studentSurname);
    }

    // Set forename.
    public void setForename(String newForename) {studentForename = newForename;}

    // Set surname.
    public void setSurname(String newSurname) {studentSurname = newSurname;}

    //
    public double averageMark(int courseworkMark, int testMark){
        return (courseworkMark+testMark)/2;
    }

    // Grab the forename
    public String grabForename(){
        return studentForename;
    }

    // Grab the surname
    public String grabSurname(){
        return studentSurname;
    }

    // Grab the full name
    public String grabFullName(){
        return studentForename + "" + studentSurname;
    }
}

4 个答案:

答案 0 :(得分:1)

import java.util.ArrayList;

public class Main {
    static ArrayList<Student> studentList = new ArrayList<>();

    public static void main(String []args) {
        addStudent("Adam", "Goldsmith", 70, 50);
        addStudent("John", "Smith", 20, 40);
        addStudent("Lewis", "Peterson", 90, 85);

        for (Student obj: studentList){
            System.out.println("Name: " + obj.studentForename + " "+ obj.studentSurname);
        }
    }

    public static void addStudent(String forename, String surname, int coursework, int test) {
        Student newStudent = new Student(forename, surname);
        newStudent.setForename(forename);
        newStudent.setSurname(surname);
        newStudent.averageMark(70, 65);

        studentList.add(newStudent);
    }

}

这不是由于未定义的大小,而是因为您尝试访问它而不用从静态方法创建对象。
所以只需在它之前编写static即可。

答案 1 :(得分:1)

您的代码应如下所示,尤其是使用Java encapsulation

的Student类
public class Student {

   private String studentForename;
   private String studentSurname;
   private int courseworkMark;
   private int testMark;

    public Student(String studentForename, String studentSurname, int courseworkMark, int testMark) {
        this.studentForename = studentForename;
        this.studentSurname = studentSurname;
        this.courseworkMark = courseworkMark;
        this.testMark = testMark;
    }   
    public void setStudentForename(String studentForename) {
        this.studentForename = studentForename;
    }
    public String getStudentSurname() {
        return studentSurname;
    }
    public void setStudentSurname(String studentSurname) {
        this.studentSurname = studentSurname;
    }
    public String getStudentForename() {
        return studentForename;
    }  
    public double averageMark(){
        return (this.courseworkMark + this.testMark)/2;
    }
    public String grabFullName(){
        return studentForename + " " + studentSurname;
    }
}

然后通过您的Main class进行测试:

public class Main {    
    public static void main(String []args) {
        ArrayList<Student> studentList = new ArrayList<>();
        studentList.add(new Student("Adam", "Goldsmith", 70, 50));
        studentList.add(new Student("John", "Smith", 20, 40));
        studentList.add(new Student("Lewis", "Peterson", 90, 85));

        for (Student obj: studentList){
            System.out.println("Name: " + obj.getStudentForename() + " "+ obj.getStudentSurname());
        }
    }
}

答案 2 :(得分:0)

上面的答案回答了您的问题,但是关于Java中静态与非静态修饰符的字眼很少

静态方法的特征

  • 使用类(className.methodName)调用静态方法,而不是使用实例引用(新instanceOfClass = class; instanceOfClass.methodName。)
  • 静态方法不能使用非静态实例变量:静态方法不能引用该类的任何实例变量。静态方法不知道要使用哪个实例的变量值。
  • 静态方法不能调用非静态方法:非静态方法通常使用实例变量状态来影响其行为。静态方法看不到实例变量的状态,因此,如果您尝试从静态方法中调用非静态方法,则无论非静态方法是否使用实例变量,编译器都会抱怨。

非静态方法

  • 非静态方法的名称前没有关键字static。

  • 一个非静态方法属于该类的一个对象,您必须创建该类的实例才能对其进行访问。

  • 非静态方法可以访问任何静态方法和任何静态变量,而无需创建类的实例。

因此,最好考虑是否需要将studentList定义为static或no,并相应地修改代码。

P.S。以上内容摘自here

答案 3 :(得分:0)

静态(全局,类级别)和非静态(该类的实例,一个对象)之间的区别很重要。

通过new Main()创建对象可以处理该对象及其字段。

静态 void main(String[])是应用程序的单个入口点。

在main内部,您只能访问静态字段和静态方法。这样很麻烦。

package srivastava.arpit; // In a directory path srivastava/arpit/

import java.util.ArrayList;

public class Main {

    private ArrayList studentList = new ArrayList<>();

    public static void main(String []args) {
        Main main = new Main();
        main.execute();
    }

    private void execute() {
        addStudent("Adam", "Goldsmith", 70, 50);
        addStudent("John", "Smith", 20, 40);
        addStudent("Lewis", "Peterson", 90, 85);

        for (Student obj: studentList){
            System.out.println("Name: " + obj.studentForename + " "+ obj.studentSurname);
        }
    }

    public void addStudent(String forename, String surname, int coursework, int test) {
        Student newStudent = new Student(forename, surname);
        newStudent.setForename(forename);
        newStudent.setSurname(surname);
        newStudent.averageMark(70, 65);

        studentList.add(newStudent);
    }
}