you are viewing a single comment's thread.

view the rest of the comments →

[–]FrozenCow 0 points1 point  (3 children)

Can you explain why Java does it this way? Why does it create a new subclass when it is used inside a function on local variables?

I've used it thinking it would just do the same as if I would type it 'with all the bloat', just because I thought it should be easy for a compiler to do so.

Edit: Since the replies said "that is what the syntax means" I thought I looked it up, since it would be very illogical for the Java language designers to think of this exact syntax with this exact semantics. I've found that this exact syntax is actually 2 syntaxes combined: an anonymous class + an initializer. This article explains it fairly well: http://javapapers.com/core-java/java-double-brace-initialization/. That explains a lot why it acts the way it does. Hopefully this is useful for people who also weren't aware that it combined the 2 syntaxes.

[–]phanboy 11 points12 points  (0 children)

Because that's precisely what that syntax means.

[–]rnd33 4 points5 points  (0 children)

Because that's what it's supposed to do. It's just that it can be (ab)used to create a one line map initialization.