| 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: 774
|
|
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: 774
|
|
|
It depends on code requirement
|
 |
Ninad Kulkarni
Ranch Hand
Joined: Aug 31, 2007
Posts: 774
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
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
|
 |
 |
|
|
subject: Use of Static methods in Utility Class
|
|
|