我刚刚开始学习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;
}
}
您的代码应如下所示,尤其是使用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类进行测试:
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());
}
}
}
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);
}
}
这不是由于未定义的大小,而是因为您试图访问它而不从静态方法创建对象。 所以只需在它之前编写静态就可以了。
以为上面的答案回答了你的问题,但是关于java中静态修改和非静态修改的几句话
静态方法的特点
非静态方法
因此,您最好考虑是否需要将studentList定义为static或no,以及相应地修改代码。
附:上面写的是从here
静态(全局,类级别)和非静态(该类的实例,对象)之间的区别很重要。
通过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);
}
}