开发者

non-static method cannot be referenced from a static context, testing sort method [duplicate]

This question already has answers here: Closed 11 years ago.

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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜