7

While browsing through some code for a library I'm using, I came across this code snippet:

public class SomeClass {

     private static final class Null {
        /* ... */
     }

     public static final Object NULL = new Null();

}

Is this a common practice to have a special NULL object for a class that gets used instead of Java's null? What are the upsides to doing this instead of using Java's built in null?

The main reason I'm curious is that while using the library, I kept checking whether SomeClass was null, without realizing that they were using a special NULL object to denote a null object of SomeClass.

EDIT: For those wondering, the exact code from the source 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
  • 10,052
  • 12
  • 47
  • 78
  • 4
    How is it used? There is the [null object pattern](http://en.wikipedia.org/wiki/Null_object_pattern), but that primarily makes sense for an class which actually has some user-defined methods. – Mark Peters Oct 28 '11 at 20:43
  • @MarkPeters: Whenever a null object would be expected, SomeClass.NULL is passed instead. In this case, the would-be object is called JSONObject and is used to denote whenever there is a null JSON value. – Ivan Oct 28 '11 at 20:45
  • But you're obviously paraphrasing here...are you sure the real code's `Null` class wasn't a subclass of some other class, or an implementer of an interface? – Mark Peters Oct 28 '11 at 20:46
  • The code within `SomeClass` was left unchanged. `public static final Object NULL = new Null();` and `private static final class Null {` are the exact lines found in the source. – Ivan Oct 28 '11 at 20:49
  • I would assume [this](http://www.json.org/javadoc/org/json/JSONObject.html) is the class/library? The comment on that field is very vague: *"It is sometimes more convenient and less ambiguous to have a NULL object than to use Java's null value."* Either way, this starts to make a bit of sense seeing as this is primarily a serialization library. – Mark Peters Oct 28 '11 at 20:55
  • `equals` returning true for null looks slippery to me. ["No object is equal to null"](http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html) – gnat Dec 25 '11 at 22:00

5 Answers5

10

The null object pattern can occasionally be a useful one - so you don't use a null reference, but have an actual object with neutral behaviour. However, the implementation you've given there doesn't make sense:

  • Null doesn't extend SomeClass, which I'd expect it to
  • NULL is declared as Object, not SomeClass.

So as it is, I'd say that code is useless. But when implemented properly, the pattern can be useful.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

This is the "null object pattern". The advantage is that you don't have to explicitly check for null, avoiding NPEs.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • That doesn't help if you're explicitly checking against this single object though...typically the "null object" would be declared in a variable of some specific type. You wouldn't just have a "Null" class. – Mark Peters Oct 28 '11 at 20:46
  • 1
    @MarkPeters Partially agreed; I don't get this implementation, but it still avoids NPEs. That said, we can't see the implementation, so I'm not comfortable saying too much about it. – Dave Newton Oct 28 '11 at 20:51
  • Yeah, checking for `NULL` is a huge improvement from checking for `null`. I mean, it's written in CAPS, so of course it's huge! – Thomas Eding Oct 28 '11 at 23:05
  • I misread what you had written. In any case, the NULL in the given code offers only the functionality of `Object`. I don't think that warrents the null object pattern. (Hey! that's NOP!) – Thomas Eding Oct 28 '11 at 23:25
  • @trinthis Agreed, this is a bizarre pseudo-implementation. – Dave Newton Oct 28 '11 at 23:34
1

A better name for their class would have been Empty or similar. Using variant of java keywords is not a good idea - it leads to confusion... and questions like these.

Also, usually the empty object is a special instance of the class:

public class SomeClass {

     public static final SomeClass EMPTY = new SomeClass("parameters that make it empty");
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

This depends on usage. There can be NULL classes which, for example, represents a Null data type which is NOT the same as a null in java. An example is here: http://www.snmp4j.org/doc/org/snmp4j/smi/Null.html

Otherwise, it would not be idiomatic java to wrap a null value in a NULL object.

jayunit100
  • 17,388
  • 22
  • 92
  • 167
1

Null object pattern.

I have used it once on of my projects and wouldn't do that again, because it be tricky when it comes to serialization

Wojciech Owczarczyk
  • 5,595
  • 2
  • 33
  • 55