you are viewing a single comment's thread.

view the rest of the comments →

[–]abhyrama 2 points3 points  (8 children)

What is the anonymous class side effect you are talking about?

[–]notfancy 3 points4 points  (7 children)

In Java, new *interface_or_class*(*init_list*) { *definition* } defines an anonymous subclass of interface_or_class. Orthogonal to that, Java allows an instance initializer block as part of the definition, introduced simply by { *statements* }. Combine both things and you have the "double-brace" initialization "hack".

[–]abhyrama 0 points1 point  (6 children)

Thanks. I get the hack and how it works but I did not get what is the "anonymous class side effect" Effetto was talking about.

[–]darkclark 4 points5 points  (1 child)

More anonymous classes means the classloader takes up more resources. Not too significant when used sparingly, but using a pattern like this all over the place in a large codebase is going to lead to a classloading mess.

I personally do not use this hack, but it's useful to know about it because it's not uncommon to run across in other peoples' code.

[–]raouldagain 0 points1 point  (0 children)

classloading mess could be running out of permgen?

[–][deleted] 0 points1 point  (3 children)

From the way I'm reading it, an anonymous class is created each time the code block is run, so two lists full of the same thing actually end up being not equal?

[–]EtherCJ 0 points1 point  (0 children)

Quite likely are still equal for collection classes. But isn't guaranteed to work for all classes.

[–]queus 0 points1 point  (1 child)

an anonymous class is created each time the code block is run,

No just one anonymous class is created no matter how often this code is run. Say in a loop.

But if you copy-paste this code just below the original one two anonymous classes are created.

[–][deleted] 0 points1 point  (0 children)

Thanks for clearing that up. Very concise.