• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static helpers - good or bad?  RSS feed

 
chris webster
Bartender
Posts: 2407
36
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wading back into Java after many years doing other things, and I have a question about using static methods/classes for "helpers". Basically, I have a class that will provide some helpful string formatting methods to one or two other classes. The formatter class has no internal state, apart form a couple of static constants, and as far as I can tell all the methods could be static as well because all they do is receive a string and return a formatted version of the input string. In fact, I can't see why I would ever need to instantiate the thing if the methods are static anyway.

But is it a good idea to make this stuff static? This thing may be called quite a lot, as it is applied to data that has been fetched from a database. I know Singletons are now regarded as a Bad Thing (although this isn't a Singleton), but I've been messing about with functional programming and immutable state recently, so I've kind of lost perspective on whether my little formatter should be static or not.

Any tips?
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a utility class there is nothing wrong with all its members being static. Give it a private constructor and never call it, so it is uninstantiable.
 
chris webster
Bartender
Posts: 2407
36
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the prompt and clear advice, Campbell. "Uninstantiable" - that's one to try saying after a few pints, eh?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd only worry about this if I was likely to ever want to use a different implementation. Using static methods means you're coupled directly to that particular class and those particular methods. From your description, it doesn't sound like that's likely to be a problem here.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chris webster wrote:But is it a good idea to make this stuff static?

Probably the only thing you can do.

I'd first make sure that you're not re-inventing the wheel, but that aside, I've already done precisely what you're thinking about; and, in keeping with the Java "convention", mine's called 'Strings'. I also have an 'Arrayz' class, and a 'Collectionz' one too...

Winston
 
chris webster
Bartender
Posts: 2407
36
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the confirmation, guys, static it is.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrayses, surely.

And yes, I am having a couple of pints
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!