-1

Error is pointing to variable record : non-static variable record cannot be referenced from a static context.

public class RecordOption {
    // global variable
    String[][] record = new String[10][3];

    // addRecords method
    public static void addRecords(String studentRecords) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter Student Number:");
        String studNumber = br.readLine();
        record[0][0] = studNumber;
    }
}
Dici
  • 25,226
  • 7
  • 41
  • 82
  • Look at the "Related" section at the right. – JB Nizet Sep 14 '14 at 15:28
  • You need to understand what `static` vs `non-static` mean first. `static` means associated with the class. `non-static` means associated with an object. `record` in this case is `non-static` which means it can only be accessed from an object. The way you are trying to access it is in a static way. That's why the compiler is not happy. – Multithreader Sep 14 '14 at 15:29
  • how/ what changes should I need to do/ – Pearl Jules Pilla Sep 14 '14 at 15:37

1 Answers1

2

The record variable must be static.

non-static variables can only be accessed from a non-static method, while static variables can be accessed from both non-static and static methods.

If you want to access non-static variable, you first need an instantiated object the variable is associated with. But if you want to access static variable, you just need the class.

jjurm
  • 501
  • 6
  • 11