I don't like this syntax. It disguises the fact that you have an instance
initializer. I'm used to scanning for a pair of braces at the top level of a
class to see if there is an instance initializer, but those braces were
camouflaged by the outer braces.
I prefer the second version, with the comment included!
Rob
On Feb 2, 2011, at 8:37 AM, Alexey Zinger wrote:
> Real quick note on something that came up in the discussion in Alternative
> Views in Software Development. Namely, the "double bracket" syntax. There
> seemed to be some confusion about it. {{}} is not a new or separate language
> construct. It's been there since the days of anonymous inner classes.
> Despite looking a little strange when written as follows, it's just normal
> Java.
>
> public Map<String, Integer> getAges()
> {
> return new HashMap<String, Integer>()
> {{
> put("kitty", 7);
> put("puppy", 2);
> put("fishy", 3);
> }}
> }
>
> What it really is is an anonymous inner class that extends HashMap and has an
> instance initializer block (as opposed to static initializer block). Let's
> rewrite above in a clearer fashion:
>
> public Map<String, Integer> getAges()
> {
> return new HashMap<String, Integer>()
> {
> // static initializer
> static
> {
> // doesn't make sense and may not be legal in an anonymous inner
> class
> }
>
> // instance initializer
> {
> put("kitty", 7);
> put("puppy", 2);
> put("fishy", 3);
> }
> }
> }
>
> More reading on the subject:
> http://download.oracle.com/javase/tutorial/java/javaOO/initial.html
>
> Alexey
> http://azinger.blogspot.com
> http://bsheet.sourceforge.net
> http://wcollage.sourceforge.net
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.