File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

 
subodh k kumar
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David that was a nice explanation .
 
pete stein
Bartender
Posts: 1561
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(They're not always, though; they'll have synchronized pools, counters, etc.)
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic