While looking at the code for the library that I am using, I came across this piece of code:
public class SomeClass { private static final class Null { } public static final Object NULL = new Null(); }
Is it common practice to have a special NULL object for a class that is used instead of Java null ? What are the benefits of this, instead of using Java built in to null ?
The main reason I'm curious is that, while using the library, I continued to check if SomeClass was empty, not realizing that they were using a special NULL object to denote the null SomeClass object.
EDIT: For those who are wondering, the exact source code is:
public class JSONObject { private static final class Null { protected final Object clone() { return this; } public boolean equals(Object object) { return object == null || object == this; } public String toString() { return "null"; } } public static final Object NULL = new Null(); }
Ivan
source share