George W. Cherry said:
What's you gonna do with a if you could
do what you can't do? In other words,
an instance of a class with all static members
ain't gonna be very useful.
I've actually thought about this subject a lot. I even thought of proposing
to Sun that instead of using the convention that you add a private
constructor that instead you could declare the class both abstract and
final, which would eliminate any possibility of creating an instance.
But then I began to ask myself what is the actual problem I am trying to
prevent. Who cares if someone actually creates an instance of an object that
has no instance fields or methods other than those in Object? It doesn't
cause any harm so why take any deliberate steps to prevent it? So I settled
on just not declaring a constructor. If someone wants to use the default
constructor I didn't feel a need to prevent that.
But I am beginning to change my mind and the reason is that I am starting to
use test coverage tools and they report that default constructor as not
having been executed. I don't have a test for it and it seems silly to add a
test to check if it has the default constructor when there is no purpose to
having a constructor on this class. If I switch to a private constructor the
situation is worse because then there is no way to even write a test that
will exercise it. I'm thinking about switching to the convention of
declaring a public constructor that throws an exception. If the class does
that then it makes sense to test for that and then I get my coverage up to
100%.