Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • 1
    This will create an extra object EMPTY along with each object. Will this affect the performance?
    – Sp4Rx
    Commented Mar 5, 2019 at 0:34
  • 4
    Empty is a static field here, so it shouldn't be a problem. Commented Jul 25, 2019 at 17:13
  • 1
    @MartinTarjányi but equals() method in java compares instances and not their field values.. you'll need to override equals() and hashCode(). Commented Jan 15, 2020 at 6:09
  • 4
    that's why I used the lombok annotation Commented Jan 15, 2020 at 6:10
  • I find myself needing to do something similar to the OP’s question and my thoughts turned first to the equals method. I originally thought to have an isEmpty method that compared to a new instance of the class, but I like the idea of using a static final object more. Nice solution!
    – Calvin P.
    Commented Feb 22, 2023 at 11:51