mohammed aadhil mohammed aadhil - 3 months ago 21
Java Question

Why Arrays class' methods are all static in java

I was going through java doc and I learned that methods in 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.

Answer

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.