Excuse my ignorance. I’m a beginner:
Why is the following code giving me the following compiling error?
[line: 16] non-static variable x cannot be referenced from a static context
public class average{
int [] numbers = {2,3,4,5,6};
double x = averageMark(numbers);
public static double averageMark(int [] numbers){
int sum = 0;
double average = 0.000;
for (int i = 0; i < numbers.length; i++){
sum = numbers [i] + sum;
average = sum/numbers.length;
}
return average;
}
public static void main (String [] args){
System.out.println(x);
}
}
The error says it all
you have to either make it
xstatic variable.or create an instance of Average and access it.
Btw, its a convention that your class names should start with Uppercase.
as @ mgaert noted you need to make numbers array static as well, cuz you use it in a static method.