Double brace initialization is a combination of two separate process in java. There are two { braces involved in it. If you see two consecutive curly braces { in java code, it is an usage of double brace initialization.
First brace is creation of an anonymous inner class. Without considering the second brace if you see the first brace alone then its nothing new for us. We have created many anonymous inner classes in such a way.
Second brace is an initialization block. That too you have seen in it a class for initialization. You may have used a static initialization block. When you use the initialization block for an anonymous inner class it becomes java double brace initialization. The inner class created will have a reference to the enclosing outer class. That reference can be used using the ‘this’ pointer.
class JavaPapers { .... .... add(new JPanel() {{ setLayout(...); setBorder(...); ... }} ); }
Using double brace initialization, we can initialize collections. Though people say, it is easier to initialize a constant collection using double brace initialization. I don’t see any advantage in using double brace initialization to initialize collections. In jdk 1.7 there is going to be an exclusive construct for that. Then java double brace initialization will become completely obsolete.
... myMethod( new ArrayList() {{ add("java"); add("jsp"); add("servlets"); }} ); ...
Serialization of non-static inner class has an issue. The outer class will also get serialized. Because the inner class has got a reference to the outer class and it is implicit. Since it is implicit, it cannot be marked as transient. This issue is also applicable for double brace initialization.
Comments are closed for "Java Double Brace Initialization".
Till now I don’t know this double brace initialization!
Man you are amazing… thanks thanks..
This is useful, but try to avoid it with classes where equals and hashCode are important. A class whose equals(Object o) method checks that this.getClass() == o.getClass() will fail badly with instance initializers.
X x0 = new X() {{ a = 3; }};
X x1 = new X() {{ a = 3; }};
assert x0.equals(x1); // fails
What about Java’s PermGen space?
What’s about PermGen memory space? Every time you’re using double brace initialization you’re creating a new subclass which has to be loaded.
it’s amazing yar…
double {{ intialization ……
This is a neat and useful trick.. But is really anti-pattern when you look closer. (Following the pattern of least surprise)
You make a new anonymous class each time you do this – causing surprise around this.getClass(). As a previous commenter pointed out, this causes havoc with .equals() .hashCode() and even logging. Additionally, you clog up your classloader (and permgen)
tl dr;A neat syntatic trick but harmful.
It is really useful to handle&crack the interviews…I’m very inspired that blog..
private static final Set VALID_CODES = new HashSet();
static {
validCodes.add(“XZ13s”);
validCodes.add(“AB21/X”);
validCodes.add(“YYLEX”);
validCodes.add(“AR2D”);
}
——
using double brace
private static final Set VALID_CODES = new HashSet() {{
add(“XZ13s”);
add(“AB21/X”);
add(“YYLEX”);
add(“AR2D”);
}};
I like it, it was new thing for me. Thanks
Good
very nice article.
Nice Stuff.
Nice article. Thanks!
Nice Article and very useful to improve..
technical knoweldge of java
its always a pleasure to read this blog.
good point known to me.
Thanks
1st brace creates Anonymous Inner class
2nd brace is called instance initializer, because it is declared within the instance scope of the class.
Initialization block is invoked before constructor is called.
And this works only for non final class because it needs to create an anonymous class which we cannot do in final classes.
And there are 2 prominent issues with double brace initialization.
1. creates problem for classes which uses equals and hashCode.
2. Serialization of this class is a problem as it is non static.
Thanks
really good stuff on java.
i m reading this blog for the first time…
comment avoir la version en Français svp.
merci
j’attend votre réponse avec impatience
Knowing that something like this exist in java was great
Sir ,
this double brace initialization seems to be like with() in .net.isn’t so?
or am i miss-understood?
Perfect! this helped me a lot to debug my serialization issue!!
Thanks
Hi,
How to send and receive sms from/to java web application.?What package helps us to do so?
[…] a Hashtable in java is created using the empty constructor Hashtable(). Which is a poor decision and an often repeated mistake. Hashtable has two other […]