0

Good evening everyone. I'm working my way through this and have hit a block in regards to using a class inside another class. Eclipse informs me that the error is a result of trying to call a static reference to a non-static method. All I am trying to do is use the method "reverse" multiple times in one program. I know that the issue is in the "reverse" class, and am looking to find the correct way format the class to accept input from the code in the main method.

public static void main (String [] args){
      //creating a test array
      int [] myArray = {0, 1, 2, 3, 4, 5, 6,7 ,8 ,9};
      int [] myArrRev = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
      reverse (myArray);
      if ( Arrays.equals(myArray, myArrRev))
         System.out.println("reverse worked for 10 elements.");


      int [] myArray2 = {0, 1, 2, 3, 4, 5, 6,7 ,8};
      int [] myArrRev2 = { 8, 7, 6, 5, 4, 3, 2, 1, 0};
      reverse (myArray2);
      if ( Arrays.equals(myArray2, myArrRev2))
         System.out.println("reverse worked for 9 elements.");


      int [] myArray3 = {0};
      int [] myArrRev3 = {0};
      reverse (myArray3);
      if ( Arrays.equals(myArray3, myArrRev3))
         System.out.println("reverse worked for 1 element.");

      int [] myArray4 = {};
      int [] myArrRev4 = {};
      reverse (myArray4);
      if ( Arrays.equals(myArray4, myArrRev4))
         System.out.println("reverse worked for 0 elements.");
   }




   void reverse( int arr[] ){
      for(int i = 0; i < arr.length / 2; i++)
         {
         int temp = arr[i];
         arr[i] = arr[arr.length - i - 1];
         arr[arr.length - i - 1] = temp;
         }
   }


}
J Welch
  • 25
  • 8

1 Answers1

2

Change the method declaration to:

   static void reverse( int arr[] )
CConard96
  • 874
  • 1
  • 7
  • 18
  • is there a way to do this without making it static? i tried removing "static" from the main method, but that obviously wont work. – J Welch May 13 '16 at 02:23
  • @JWelch What's the problem with making it `static`? But yeah, you can create an instance of your class, then use that instance to call the non-static method. But why? What's the point of that? The method is not using *any* fields of the class, so why? – Andreas May 13 '16 at 03:17