Reputation: 470
I was going through Java documentation, and I learned that methods in the Arrays
class in Java are all static. I don't really understand the reason behind why they made it static.
For example, the following code violates the OO approach, because if I have a type, 'X', then all the methods which acts on it should be inside it:
int[] a = {34, 23, 12};
Arrays.sort(a);
It would be better if they have implemented the following way:
int[] a = {34, 23, 12};
a.sort();
Can anyone explain me a bit on this?
Upvotes: 33
Views: 4314
Reputation: 2669
The Arrays
class contains methods that are independent of state, so therefore they should be static
. It's essentially a utility class.
While OOP principles don't apply, the current way is clearer, concise, and more readable since you don't have to worry about polymorphism and inheritance. This all reduces scope, which ultimately reduces the chances that you screw something up.
Now, you may ask yourself "Why can't I extend the functionality of an array in Java?". A nice answer is that this introduces potential security holes, which could break system code.
Upvotes: 1
Reputation: 596
For me this is the perfect solution. I have an array, and I have a class, Arrays, which operates over the data in the array. For example, you may want to hold some random numbers and you will never want to sort or any other utility method you will receive behavior which you don't want. That's why in code design it is good to separate data from the behavior.
You can read about the single responsibility principle.
Upvotes: 1
Reputation: 162831
Good observation. Observe also that not every array can be sorted. Only arrays of primitives and Objects which implement the Comparable
interface can be sorted. So a general sort()
method that applies to all arrays is not possible. And so we have several overloaded static methods for each of the supported types that are actually sortable.
@Holger correctly points out in the comments below that one of the overloaded static methods is indeed Arrays.sort(Object[])
but the docs explicitly state:
All elements in the array must implement the
Comparable
interface.
So it doesn't work for Objects that don't implement Comparable
or one of its subinterfaces.
Upvotes: 20
Reputation: 18303
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.
Upvotes: 2
Reputation: 10064
Arrays are kind of like second-class generics. When you make an array it makes a custom class for the array type, but it's not full featured because they decided how arrays would work before they really fleshed out the language.
That, combined with maintaining backwards compatibility, means that Arrays are stuck with an archaic interface.
It's just an old part of the API.
Upvotes: 2
Reputation: 22271
First of all, Arrays
is an utility class, which does exactly that: exposes static methods. It is separate from any arr[]
instances and has no OO relation to it. There are several classes like that, like Collections
or various StringUtils
.
Arrays are collections, they are used to store data. Arrays.sort()
is an algorithm which sorts the collection. There may be many other algorithms which sort data in different way, all of them would be used in the same way: MyAlgorithm.doSthWithArray(array)
. Even if there was a sort()
method on an array (it would then have to be a SortableArray
, because not all Objects can be sorted automatically), all other algorithms would have to be called the old way anyway. Unless there was a visitor pattern introduced... But that makes things too complicated, hence, there is no point.
For a java Collection there's Collections.sort()
, even in C++ there is std::sort
which works similarly, as does qsort
in C . I don't see a problem here, I see consistency.
Upvotes: 13
Reputation: 533670
In Java there is no way to extend the functionally of an array. Arrays all inherit from Object
but this gives very little. IMHO This is a deficiency of Java.
Instead, to add functionality for arrays, static utility methods are added to classes like Array
and Arrays
. These methods are static
as they are not instance methods.
Upvotes: 41
Reputation: 847
Static Methods are sometimes used for utility purpose. So Arrays is utility class for general purpose array operations. Similarly, Collections is also Util class where utility methods are given.
Upvotes: 3