0

I've just started to learn Java a month or so ago, and now have a problem with "non-static variable studentList cannot be referenced from a static context". I'm trying to have a separate method from main to populate the list of students, instead of copy pasting stuff from addStudent for each student; but I cannot get the methods to write to the ArrayList. (Error:(14, 27) java: non-static variable studentList cannot be referenced from a static context). I understand how the array is not static because it has an undefined size, but how could I make it work as is? Is there any better approach? Could I have the array be part of the main method and then have it passed on to addStudent, if so how?

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);
    }

}

and my "Student" Class:

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;
    }
}
DarkXylese
  • 3
  • 1
  • 5
  • Non-static variables belong to your instance (every time you use `new Student()` you create one). Static variables belong to your class, which means they are shared across all instances. You can't use an instance variable in a static context because you don't have an actual instance to begin with. If you were to pass one to the method by its parameters, then it would work – Ayrton Feb 19 '19 at 11:50
  • 2
    TLDR: make your `studentList` variable `static` to make it work – Ayrton Feb 19 '19 at 11:51
  • Possible duplicate of [cannot make a static reference to the non-static field](https://stackoverflow.com/questions/8101585/cannot-make-a-static-reference-to-the-non-static-field) – Federico Ciuffardi Feb 19 '19 at 11:54

4 Answers4

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);
    }

}

It was not due to undefined size but was because you were trying to access it without creating an object from a static method.
So just write static before it and it will work.

Haripriya
  • 822
  • 1
  • 14
  • 27
fuzious
  • 400
  • 5
  • 19
1

Your code should look like this, especially your Student class using java encapsulation

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;
    }
}

And then testing via your 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());
        }
    }
}
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

thought the above answer answers you question, but few words about static vs non static modifiyers in java

Characteristics of Static methods

  • A static method is called using the class (className.methodName) as opposed to to an instance reference (new instanceOfClass = class; instanceOfClass.methodName.)
  • Static methods can’t use non-static instance variables: a static method can’t refer to any instance variables of the class. The static method doesn’t know which instance’s variable value to use.
  • Static methods can’t call non-static methods: non-static methods usually use instance variable state to affect their behaviour. Static methods can’t see instance variable state, so if you try to call a non-static method from a static method the compiler will complaint regardless if the non-static method uses an instance variable or not.

Non-Static methods

  • A non-static method does not have the keyword static before the name of the method.

  • A non-static method belongs to an object of the class and you have to create an instance of the class to access it.

  • Non-static methods can access any static method and any static variable without creating an instance of the class.

So you'd better think if you need to define studentList as static or no, and if modify your code accordingly.

P.S. above written is taken from here

Hakob Hakobyan
  • 1,111
  • 8
  • 15
0

The difference between static (global, class level) and non-static (a instance of that class, an object) is important.

Creating an object by new Main() allows to work on that object and its fields.

The static void main(String[]) is the single entry point to the application.

Inside the main you have access only to static fields and static methods. So that is cumbersome.

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);
    }
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138