0

Here is the code itself

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

}

Some println commands are missing in the main method. But when I try to add the 5 students to my ArrayList, I get the error "Cannot make a static reference to the non-static field rayList"

S.R.I
  • 1,860
  • 14
  • 31

4 Answers4

6

You're trying to execute code outside of executable context. Code can only be executed from a method, static initialiser or instance initialiser (Thanks NickC) context.

Try moving it into the main method to start with...

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

Updated based on feedback

Your first error Cannot make a static reference to the non-static field rayList was generated because the rayList was not declared static, but you were trying to reference it from a static context.

// 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 is declared as a "instance" field/variable, meaning that it requires an instance of the declaring class (Student) before it has meaning.

This could be resolved by...

Creating an instance of Student in main and accessing it via that instance, for example...

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

Personally, I don't like this, rayList doesn't really belong with Student, it adds no value. Can you imagine having to create an instance of Student before you add any to the List?

I also don't like accessing instance fields directly, but that's a personal preference.

Making rayList static

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

This is a viable option, but would require more context before it could be deemed good or bad. I personally feel that static fields can cause more problems than they solve, but again, that's a personal opinion and your context may deem a reasonable solution.

Or, you could create a local instance of the List within the context of the static method as seen in the first example.

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

As I said, which one you choice to do will come down to you, I would personally prefer the last two, but that's me.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • You've fixed the problem (there were 2) but only explained one part of why it was broken, and not the one the OP asked about. And no, static initializers and methods are not the only contexts where code can be executed. There are also instance initializers. – Nicole Sep 27 '13 at 07:08
  • @NickC 1st Thanks for the instance initialisers, 2nd, can you explain "and not the one the OP asked about" a little further? As I see it, the OP wasn't sure why they were getting errors with their code, which is because of the execution context... – MadProgrammer Sep 27 '13 at 07:13
  • 2
    It seems to me that the first error the compiler caught was that `rayList` was not static but referred to from static method `main`. I think the OP should know about that as well, though it's important that you've caught the other error as well. – Nicole Sep 27 '13 at 07:20
  • @NickC Thank you for taking the time to provide feedback so I could learn something new and improve my answer and for your suggestions – MadProgrammer Sep 27 '13 at 07:30
1

This is because you are accessing non static field inside static function. In your case.

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

And you cannot access instance variable w/o creating instance of a class.

Instead use,

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));
}
Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79
1

That error means that rayList is not static (in other words, it is a member of class Student) but your main method is:

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

You actually can't make a list of Students as a member of Student because you will either get a stack overflow or run out of memory, depending on how you create it, so I'm guessing you wanted rayList to be static. That would make rayList no longer a member of Student.

You also cannot add to your ArrayList the way you are outside of an initializer block or method.

See here for more on initializer blocks:

Or you could move everything referring to rayList inside your main method.

Community
  • 1
  • 1
Nicole
  • 32,841
  • 11
  • 75
  • 101
0

Make the rayList variable static to use it in main method like this :

Then add the objects in main method and use it :

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.
}
Vimal Bera
  • 10,346
  • 4
  • 25
  • 47