File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Use of Static methods in Utility Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use of Static methods in Utility Class" Watch "Use of Static methods in Utility Class" New topic
Author

Use of Static methods in Utility Class

Sachin Deokar
Ranch Hand

Joined: May 09, 2008
Posts: 41
Is the use of static method in Utility classes a good idea? If not then what are other better ways to write utility methods?

I am not too fond of using singleton classes or static methods. Would appreciate your suggestions.

Regards,
Sachin Deokar
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
What is it that you don't like about utility classes and/or static methods?
... Jim ...


BEE MBA PMP SCJP-6
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 784

Sachin Deokar wrote:Is the use of static method in Utility classes a good idea? If not then what are other better ways to write utility methods?

I am not too fond of using singleton classes or static methods. Would appreciate your suggestions.

Regards,
Sachin Deokar


May I know why are you not too fond of using singleton pattern or static method?


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Sachin Deokar
Ranch Hand

Joined: May 09, 2008
Posts: 41
sorry .. let me correct myself .. I dont like over use of static methods. In my last project I ended up using one too many. Thought I would ask you guys If there was any other good alternative to write utility classes.

Sachin
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 784

It depends on code requirement
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 784

Suppose if you need only single instance then use singleton pattern.
If your code requirement satisfied by static utility method then why are you using instance method? For instance method you need instance of an object.
For example if you need polymorphism then you should use instance method.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

http://www.coderanch.com/t/407852/Beginning-Java/java/why-do-we-use-static

particularly read Campbell Explanation
Sachin Deokar
Ranch Hand

Joined: May 09, 2008
Posts: 41
Thanks Guys for your Replies.

So, are static method only option to write utility methods, or can they be written/accessed any other way? any design pattern that I can follow for writing utility class?

I am just trying to get information on best possible way to write Utility Class making sure I dont end up writing error prone code that is used everywhere in the application.

Thanks again. Appreciate your help.

Sachin
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Sachin Deokar wrote:sorry .. let me correct myself .. I dont like over use of static methods. In my last project I ended up using one too many.

That indeed sounds like a code smell (a symptom that there might be something wrong with the design of your program), that the program is not really designed the object oriented way. If you're interested in designing better code, have a look at anti-patterns, there are whole books about this subject.

There are some disadvantages to the singleton pattern, some people say that the singleton is an anti-pattern - mainly because it makes unit testing hard (it adds global state to the program) and because you need to be careful with synchronization if your program is multi-threaded. You can use dependency injection instead of singletons in many cases.

If your utility methods are pure utility methods, that don't retain any state, I'd just make them static methods - but don't use too many of them.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Static methods in Utility Class
 
Similar Threads
Utility Classes in EJB
Static methods Vs "Instance Methods"
Static or non-static method?
Why is it that helper / Utility classes only contain static Methods ??
DataBase Utility class consisting of static methods