Nice idiom for uninstantiable Java classes
Sometimes we create Java classes that consist of static methods and constants only, usually to group related stateless utility routines, a la java.lang.Math. We typically prevent the client of the class from instantiating the class by making the class's zero-argument constructor private. While browsing the source of Selenium, I saw the idiom taken even further: such private constructors throw java.lang.UnsupportedOperationException always. Talk about code clearly communicating its intent! I think this is a tremendous improvement over the typical "Don't call this" javadoc comment: if executed, the constructor will reject the invocation, and says so with code instead of comments.
Written on February 2, 2005