Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Methods

 
shri kanth
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,

Why Arrays methods are static ? Is there any concept behind keeping these and Collections methods as static.

regards,
Shri

 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have a look at the Campbell Ritchie Classification of Methods. The methods in the Arrays class do not take any information from anywhere in an Arrays objecy, nor do they send any information to anything in an Arrays object. So they can be static.

You can try creating an Arrays object to see whether you can get any information into or out of it, and you can't.
So what is the point of an Arrays object? So they have prevented you creating an Arrays object in the first place.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classes java.util.Arrays and java.util.Collections are classes with utility methods for working with arrays and collections. It would not make much sense if they were not static, that would mean you have to create a dummy instance of those classes to be able to call the methods.
 
shri kanth
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Campbell.

Yes that make sense that these to be static methods. Avoiding unnecessary object creation. Thanks for the quick replies. And Campbell's classification of methods is really a good guide on deciding a method to be static or non-static.

Shrikanth
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome


. . . but be careful about saying Campbell's anything is really good
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic