I recently saw something in some code that made me curious if it was actually having some kind of optimization or performance impact. It was a line like this in a constants file:
public static final Object NULL = null;
Then, throughout the code, rather than explicitly using the null keyword, it would be referred to with Constants.NULL.
I’ve seen this kind of thing before with something like:
public static final String EMPTY_STRING = "";
… and that seemed to make at least a little bit of sense, if it’s an attempt to avoid creating lots of duplicate "" instances. But does it really make any difference with null, since it’s not actually any kind of object? Is there something I’m missing?
I don’t think that defining and using
NULLin this manner does more than add noise. Perhaps whoever wrote that code came from a C++ background, and preferred the more familiar look ofNULLovernull.The second example is also questionable, since using
""many times would not result in a separateStringobject created for every use. To quote the JLS: