Womuse1979 Womuse1979 - 20 days ago 8
Java Question

Error passing int arrays to method Total

I am new to coding and so simple mistakes aren't very oblivious to me.
The code is trying to pass an integer of arrays into the method called total and if the array is empty return zero. My eclipse crashed and so I have to use an online tool to run my java code.

It keeps saying that I have an error and I do not understand its reasoning.

non-static method total(int[]) cannot be referenced from a static context

public static void main(String []args){
int [] array = {1,2,3,4};
total(array);
System.out.println(result);

}


public int total(int[] a){
if(a== null){
return 0;
}
int result=0;
for(int i = 0; i<a.length;i++){
result+=a[i];
}

return result;
}


}

Answer

change public int total(int[] a){ to public static int total(int[] a){

use System.out.println(total(array)); as call

Comments