0
public class Main
{
    public static void Main(String[] args) 
    {
        String [] arr = {"10", "20", "30", "40", "50"};
        reverse(arr);
    }
    public String[] reverse(String[] arr)
    {
        for(int i=arr.length-1;i>=0;i--)
        System.out.print(arr[i] + "  ");
    }
}

I'm trying to reverse the array. The error says non-static method reverse(String[]) cannot be referenced from a static context

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Ahmad Khan
  • 17
  • 5
  • `reverse` needs to be `static`. – SuperStormer Apr 27 '21 at 23:55
  • Can you make a version that *is* static? – Scott Hunter Apr 27 '21 at 23:55
  • 1
    Does this answer your question? [Cannot make a static reference to the non-static method](https://stackoverflow.com/questions/4969171/cannot-make-a-static-reference-to-the-non-static-method) – Irad Ohayon Apr 27 '21 at 23:55
  • I can't make reverse static – Ahmad Khan Apr 27 '21 at 23:56
  • 2
    If you can't make `reverse` static, then does this answer your question? ["Non-static method cannot be referenced from a static context" error](https://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error) – SuperStormer Apr 27 '21 at 23:57
  • I don't know if I tried it the wrong way, but it doesn't work. – Ahmad Khan Apr 28 '21 at 00:10
  • Voting to close as a duplicate, however, the short answer is that to reference a non static method you simply need to make a new instance of the class containing the method before you call it. For example `Main yourInstance = new Main(null);` creates an instance, then you can call the non static method `yourInstance.reverse(arr);`, or you could do it all on one line like so `String[] result = new Main.reverse(arr);` – sorifiend Apr 28 '21 at 00:10
  • Also, your reverse method doesnt return anything... make it `public void reverse(...` or better yet `public static void reverse(...` oryou need to actually return the updated array `return yourReversedArray;` – sorifiend Apr 28 '21 at 00:11

0 Answers0