A weird Java trick for populating collections that you should never use!

A lot of people (myself included) complain about how verbose Java is.  This is especially true when it comes to populating collections from the Java Collection API.  Here’s what you typically see:

It’s frustrating that this is so verbose.  Wouldn’t it be better if you could do this common task in a concise way?  Well… there is actually a trick you can use in Java to do this.  It looks like this:

This has exactly the same result, but you should never do this:  This is difficult to read and most Java Engineers don’t even know how this works.  Then why did I decide to write an article about this if it’s a “worst practice”?  Well, it can be fun to know about tricks in Java that others don’t.  But more importantly, there is something to learn here.  What is this doing?  How does it work?  We are going to learn that and at the very end I’ll show you what code you should be using instead that’s the best of both worlds.

Before I explain what’s going on here, I’ve got to explain a little known concept called “Instance Initializers”.  Did you know you could do this:

As you would expect from the way I labeled these String s, this outputs:

The Instance Initializer gets run before any constructor is run, each time you initialize this class.  If you’ve never heard of Instance Initializers, it’s probably because there isn’t much use for them.  The most useful thing you can do seems to be that it lets you execute code when an anonymous inner class is created.  Anonymous inner classes don’t have names so you can’t define a constructor for them.  But you can do this:

I still wouldn’t recommend this, because — like I said — most Java developers won’t understand what’s going on here.  Here’s an even trickier example:

The output of this is

This is evidence that the super  in the Child  Constructor is called, then the Child  Instance Initializer, then the rest of the  Child  Constructor is executed.  These are some really esoteric details that you’d probably never run into in real life.  So if this part is confusing, I think you can ignore it.

Back to the original example.  What is this doing?

This is doing a few things:

  1. It is creating an anonymous inner class that extends  ArrayList
  2. It is defining an Instance Initializer in the anonymous inner class
  3. In that Instance Initializer, it is calling add  three times

The end result is a List  with three Strings  in it.  I hope this was understandable.  Please leave me comments if you need clarification on anything.

I promised I would show you a better way to achieve the same examples as above.  Here it is:

That Lists  class is part of the Google Guava library, and I highly recommend you use it if you aren’t already.

I hope you learned something interesting in this article.  If you’d like to learn more tricks like this, subscribe to my newsletter.

Join the email list for bonus content.

Leave a Reply

Your email address will not be published. Required fields are marked *