This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
The reason for asking this question is that , in a web based application if we use static methods to close Database connections ,
then if one Thread (request ) closes the connection as they are static , i think for all the other requests/Threads it will entirely close the Database connection .
The instance of Connection passed to the close() method is not the same always? So after calling an open() on the Utility class a different Connection is passed to each close() so only that particular instance would be closed.
Jesper Young wrote:Creating classes with utility methods that are all static is a common pattern that's also used in the standard Java library. Examples are classes java.lang.Math, java.util.Arrays and java.util.Collections.
It's not very object-oriented, but nevertheless it's a common thing. I've also written a number of StringUtils classes myself...
Creating utility classes that can't be instantiated or extended / partially overridden is something to be very cautious about nonetheless.
For something like a StringUtilities class this is perfectly fine, of course, but in other cases it could potentially get frustrating.
I've encountered 'utility' classes like that, which handle (file) I/O, for instance.
Without a very well thought out API and no way to create and inject a test double, writing (unit) tests to cover the code that uses such a utility can be a bit of a pain.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.