This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Debug Note < New Class (…) {{… }} initialize idiomatic meaning >

Ask questions

{{… }} What does block mean in the following code?

class X {

    private Y var1;

    private X() {
        Z context = new Z(new SystemThreadPool()) {{
            var1 = newY(); }}; }}Copy the code

Answer a

This is called double curly brace initialization

This means that you are creating an anonymous subclass, and the code inside the double brackets is basically a constructor. It is commonly used to add content to a collection because Java’s syntax for creating what are essentially collection constants is somewhat awkward. So, you can do this:

List<String> list = new ArrayList<String>() {{
  add("one");
  add("two");
  add("three");
}};
Copy the code

Instead of

List<String> list = new ArrayList<String> (); list.add("one");
list.add("two");
list.add("three");
Copy the code

I actually don’t like it, prefer to do it like this:

List<String> list = Arrays.asList("one"."two"."three");
Copy the code

So it doesn’t make much sense in that case, and it doesn’t make much sense for Maps without a handy helper.

Answer two

The “brace” indicates that you want to create an anonymous subclass, and the second brace is the object initializer.

The initializer runs before the constructor of the class, but after any super calls (and therefore after all superclass initializers).

You can also use initializers in non-anonymous classes, final if you have multiple constructors that cannot call each other.

This is a convenient way to initialize fields that require more complex initialization than the usual field initializer.

Consider this:

class X extends Y{
    private final int lulz;

    private static boolean someCondition(){... } privatestatic boolean danger() throws SomeException { ... }
    public X(A a) throws SomeException {
        super(a); 
        lulz = someCondition()? danger() : 0;
    }
    public X(B b) throws SomeException {
        super(b); 
        lulz = someCondition()? danger() : 0; }}Copy the code

It can be rewritten as:

class X extends Y{
    private final int lulz;

    private static boolean someCondition(){... } privatestatic boolean danger() throws SomeException { ... }
    { // initalizer -- might throw SomeException!
        lulz = someCondition()? danger() : 0;
    }
    public X(A a) throws SomeException { super(a); }
    public X(B b) throws SomeException { super(b); }}Copy the code

All constructors must declare that they can throw exceptions if the initializer can throw check exceptions.

Answer three

You are creating an anonymous class and using the class Instance Initializer idiom, as follows:

class X {
    private Y var1;

    private X() {
        Z context = new Z(
               new SystemThreadPool()){{// This is the initialize idiom
                       var1 = new Y();      //
                   }                        //});// BTW you are missing ")"}}Copy the code

The article translated from Stack Overflow: stackoverflow.com/questions/7…