0

Possible Duplicate:
What is the reason behind "non-static method cannot be referenced from a static context"?

public class Sorting
{
 public int[] Sort(int[] input)
 {
 //sorting algorithm
    return answer
 }

 public static void main(String[] args)
 {
 System.out.println(Arrays.toString(Sort(array to be sorted)));
 }
}

I get the non static method cannot be referenced from a static context, I forget how to overcome this as it's been a while since I have used java.

I need to create the sorting method and test it in the same file.

Community
  • 1
  • 1
SGE
  • 2,317
  • 3
  • 19
  • 16

3 Answers3

3

Option 1: Make the Sort function static

public static int[] Sort(int[] input)

Option 2: Create an instance of the class

 public static void main(String[] args)
 {
 Sorting s  = new Sorting();
 System.out.println(Arrays.toString(s.Sort(array to be sorted)));
 }
Dave S
  • 20,507
  • 3
  • 48
  • 68
  • +1: However, if the `Sort` method needs no access to member variables, it makes more sense to simply make it `static`. – Oliver Charlesworth Aug 11 '11 at 11:34
  • because the sorting algorithm is recursive, it will call itself – SGE Aug 11 '11 at 11:36
  • I dont understand why have u made an object of a class that only has a static method.. shouldnt the class itself be static and you call the function as Sorting.Sort(array) – Baz1nga Aug 11 '11 at 11:36
2

Make Sort a static method!

public static int[] Sort(int[] input)
...
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0
Arrays.toString(new Sorting().sort(array to be sorted))
animuson
  • 53,861
  • 28
  • 137
  • 147
Amareswar
  • 2,048
  • 1
  • 20
  • 36