Settings

Theme

Double Brace Initialization in Java

c2.com

38 points by angeladur 13 years ago · 27 comments

Reader

tzs 13 years ago

I know this is blasphemy in the Java world, but I'd probably use a preprocessor to transform some clean syntax into the normal Java code. I'd rather have the slight increase in build complexity over the potential issues with the extra classes and the breaking of assumption about how things compare and such.

  • guard-of-terra 13 years ago

    Your IDE would complain all the time. Seriously, you lose one of a few good parts, stellar IDE support. Why bother with Java then?

  • syjer 13 years ago

    Something like Xtend ( http://www.eclipse.org/xtend/ ) ?

  • jlgreco 13 years ago

    I have always been extremely tempted to just throw together a quick and dirty preprocessor in maybe Awk for the few personal projects that I've used Java for. It would just make everything so much nicer.

  • ExpiredLink 13 years ago

    > I'd probably use a preprocessor to transform some clean syntax into the normal Java code.

    If I were in your place, I'd just learn enough Java to be proficient. Java is a very powerful language and certainly better suited for mundane tasks than your preprocessor.

    • mindslight 13 years ago

      I think you misunderstood; he wants to avoid doing mundane tasks.

      There's a couple of extensible preprocessors that aim to do exactly this. They're called 'clojure', 'scala', and 'kawa'.

philthom 13 years ago

Check out the Immutable Maps feature, does the same with some nice syntactic sugar - http://docs.guava-libraries.googlecode.com/git/javadoc/com/g...

  • jrockway 13 years ago

    You don't even need to use the Builder directly, just use the structure's "of" method:

       ImmutableSet<Foo> foos = ImmutableSet.of(a, few, elements);
       ImmutableMap<Key, Value> map = ImmutableMap.of(k1, v1, k2, v2);
jfb 13 years ago

This just makes me glad I don't have to write Java any more.

  • irahul 13 years ago

    > This just makes me glad I don't have to write Java any more.

    You don't have to do it even if you are writing Java. This is a perfect example of clever for the sake of being clever.

    • jlgreco 13 years ago

      The issue of course is that both the clever way and the 'normal' way are not exactly pleasant compared to most other modern languages.

  • J446 13 years ago

    That depends if you mean Java the language or Java the platform. Java the language is to include immutable collection literals in JDK8:

    http://jcp.org/en/jsr/detail?id=337

    Today Java lets you do the following:

    List<String> myList = Arrays.asList("one", "two", "three");

    Scala and Groovy which target the JVM let you do things like:

    Scala: val myList = List("one", "two", "three")

    Groovy: def myList = ["one", "two", "three"]

    Then there's JRuby, Jython etc.

    • Macha 13 years ago

      > List<String> myList = Arrays.asList("one", "two", "three");

      This is often not good enough, as Arrays.asList returns some internal immutable List type. If you then tried to do something like myList.add("four"); you'd get an exception.

      What you'd actually need to do is something more like:

      > List<String> myList = new ArrayList<String>(Arrays.asList("one", "two", "three"));

      Compare that to more dynamic languages, where that example would be:

      > myList = ["one", "two", "three"]

      Yes, it's a trivial example, but it's something many programs involve all over the place, and the boilerplate adds up a tedious task.

manojlds 13 years ago

This seems to add more complexity than what it's worth. Non-final requirement and the equals problem, i'd rather ignore it all together!

LinaLauneBaer 13 years ago

I am no Java expert but they write that this syntax automatically creates a anonymous inner class - every time you create a collection instance in this way. Or am I wrong about this?

Most of the examples given are creating static objects but the last example a non-static object is created in the same way. Imagine you are doing this a couple of times then you end up with a lot of anonymous classes. Isn't there a limit on the number of classes you can have? At least it makes debugging problematic.

But I might be wrong about this since I am not a Java expert...

  • guard-of-terra 13 years ago

    In Java, you have a gazzilion of classes. You have seriolusly many classes. And if you use Clojure or Scala or JRuby, you number of classes multiplies.

    You just stuff a hundred of jars full of classes into your classpath and never look back.

    JVM hums happily and does not care about however much classes you have. No problems with debugging either because why would it be? Anonymous classes are as introspective as non-anonymous.

  • brown9-2 13 years ago

    In the majority of cases where you are deploying your java code on a server you own, having a few dozen or hundred extra classes is not a big deal. Each adds maybe a few kb to the size the jar, and as for loading costs, a class is only loaded into a running JVM once.

    The cases in which to worry about stuff like this is if you need to load the code on someone else's machine, like with an applet or Android.

  • mindslight 13 years ago

    It's one class per use of this technique. It's not redefined every time the code runs.

ansciath 13 years ago

I consider this to be an antipattern, in part for some of the reasons listed in the article. The syntactic convenience being attempted here is more appropriately encapsulated as a utility method.

muyyatin 13 years ago

It can also make UI code much more readable, but it also comes with the cost of making the bytecode much larger (each use of the double-brace initialization creates a new class).

chamakits 13 years ago

I had known of this before, and I'm fairly certain I've used it before. However, one thing I didn't consider is the equals issue commented on the site. As attractive as it may be to use, I think from now on I will refrain from using the double braces.

  • irahul 13 years ago

    > I had known of this before, and I'm fairly certain I've used it before.

    I consider myself pretty liberal when it comes to coding standards and being clever, but this is something that will badly piss me off. I can't quite put my finger on why I feel so, considering I am perfectly fine with most of the perl tricks. For example, https://gist.github.com/3392206 looks good to me(you are using Perl, you should know about list and scalar contexts).

J446 13 years ago

Collection literals for inclusion in JDK8:

http://jcp.org/en/jsr/detail?id=337

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection