0

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

}

  • You can't call an instance method for an object without having an instance. The simplest solution is to make `total` a `static` method. – Peter Lawrey Nov 15 '16 at 09:21

1 Answers1

0

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

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

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65