An array is not an object which stores state, beyond the actual values of int the array. In other words, it's just a "dumb container". It doesn't "know" any behaviour.
A utility class is a class which has just public static
methods which are stateless functions. Sorting is stateless because there's nothing remembered between calls to that method. It runs "standalone", applying its formula to whatever object is passed in, as long as that object is "sortable". A second instance of an Arrays
class would have behaviour no different, so just have the one static
instance.
As Dariusz pointed out, there are different ways of sorting. So you could have MyArrays.betterSort(array)
as well as Arrays.sort(array)
.
If you wanted to have the array "know" how best to sort its own members, you'd have to have your own array class which extends
an array.
But what if you had a situation where you wanted different sorting on different times on the the same array? A contrived example, maybe, but there are plenty of similar real-world examples.
And now you're getting complicated. Maybe an array of type T
sort differently than type S
....
It's made simple with a static utility and the Comparator<T>
interface.