mohammed aadhil mohammed aadhil - 3 months ago 14
Java Question

Why are the 'Arrays' class' methods all static in Java?

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?

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.

Comments