non-static method cannot be referenced from a static context, testing sort method [duplicate]
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 algo开发者_JS百科rithm
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.
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)));
}
Make Sort
a static method!
public static int[] Sort(int[] input)
...
Arrays.toString(new Sorting().sort(array to be sorted))
精彩评论