Search code examples
javaapache-commons

Why are WordUtil instances a bad idea?


I was poking through the Apache Commons java docs when I found this:

WordUtils instances should NOT be constructed in standard programming.

Why?

I am a novice programmer, so perhaps it's the "standard" bit that is tripping me up...


Solution

  • WordUtils is a static utility class. It provides a bunch of static methods that each performs a useful task, but there is no need to ever create an instance of WordUtils, because the class stores no state.

    You can invoke static methods directly upon the class:

    WordUtils.doSomething();
    

    There is no need to create an instance:

    WordUtils wu =  new WordUtils();
    wu.doSomething();
    

    Although this will work, creating the WordUtils object (using new) is pointless.

    If all this static stuff makes no sense, try the Java Tutorial on the topic.