Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Why is it that helper / Utility classes only contain static Methods ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why is it that helper / Utility classes only contain static Methods ??" Watch "Why is it that helper / Utility classes only contain static Methods ??" New topic
Author

Why is it that helper / Utility classes only contain static Methods ??

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Hi ,
please let me know why is it that

"Why is it that helper / Utility classes only contain static Methods"??

I got this query when i am working on a Factory Pattern in which there is a static method that will return the appropiate Object depending upon the data passed to it .

Please share your views

Thanks in advance .


Save India From Corruption - Anna Hazare.
subodh k kumar
Greenhorn

Joined: Aug 19, 2009
Posts: 16
Helper Class mainly have private constructor as well because of which it can not be extended and instantiated.So the only way to access the functionality/methods is to keep the methods static.This also helps Helper/Utility class to keep its functionality secure from unwanted overriding the method funtionality.


SCJP1.4 , SCWCD 5
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks for your reply , but i guess that is not the right answer (due to private constructor we are using static methods)

I guess that as the functionality needed is same for every user so a static method is provided , anybody correct me if i am wrong .
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's because it's assumed there's no *reason* to instantiate it (but this is arguable)--if you don't *need* anything other than static methods, why make it instantiable or provide any non-static properties/methods?

On the other hand, purely static classes are more of a pain to use when testing the classes that use them, and although there are mocking libraries that can work around this, I'm not a big fan.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

Thanks David that was a nice explanation .
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
It is my understanding that utility classes are stateless and that this is one reason for their methods to be static.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(They're not always, though; they'll have synchronized pools, counters, etc.)
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

pete stein wrote:It is my understanding that utility classes are stateless and that this is one reason for their methods to be static.


That was a good point , thanks Pete .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is it that helper / Utility classes only contain static Methods ??
 
Similar Threads
immutable classes
Class Diagram
Question about helper class of session bean
Utility Classes in EJB
Question about helper class of session bean