Java technique-double arc Initialization

Source: Internet
Author: User

Turn: http://java.csdn.net/page/af53271d-b046-48a0-8bb4-99feed68d446 #

 

Doublebraceinitialization

This is my http://www.c2.com/cgi/wiki in javaidioms? Java usage tips. I have been using Java for so many years, and it is still the first time I have seen it. It is also very practical. Although this tips may seem simple, you won't see it in any Java textbook, because it is not a real syntax specification, but a syntax usage variant. Tips also contain the deep knowledge of Java, which is the secret of a veteran.

Translated text:

The Collection framework of Java language (collections, such as list, MAP, set, etc.) does not provide any simple syntax structure, which makes it very complicated to create a constant set. We need to do this each time we create:

Define a temporary collection class variable

Create an instance with an empty set and assign it to the variable

Put data into a collection

Finally, pass the set as a parameter to the method.

For example, to pass a set variable to a method:

JavaCode
Set validcodes = new hashset ();
Validcodes. Add ("xz13s ");
Validcodes. Add ("ab21/X ");
Validcodes. Add ("yylex ");
Validcodes. Add ("ar2d ");
Removeproductswithcodein (validcodes );

You can also use the static initial method.

Java code
Private Static final set validcodes = new hashset ();
Static {
Validcodes. Add ("xz13s ");
Validcodes. Add ("ab21/X ");
Validcodes. Add ("yylex ");
Validcodes. Add ("ar2d ");
}
Private Static final set validcodes = new hashset ();
Static {
Validcodes. Add ("xz13s ");
Validcodes. Add ("ab21/X ");
Validcodes. Add ("yylex ");
Validcodes. Add ("ar2d ");
}

In fact, there are also simple methods. We can use double-brace syntax to create and initialize a new set:

Java code
Private Static final set valid_codes = new hashset (){{
Add ("xz13s ");
Add ("ab21/X ");
Add ("yylex ");
Add ("ar2d ");
}};
Private Static final set valid_codes = new hashset (){{
Add ("xz13s ");
Add ("ab21/X ");
Add ("yylex ");
Add ("ar2d ");
}};

Or

Java code
Removeproductswithcodein (New hashset (){{
Add ("xz13s ");
Add ("ab21/X ");
Add ("yylex ");
Add ("ar5e ");
}});
Removeproductswithcodein (New hashset (){{
Add ("xz13s ");
Add ("ab21/X ");
Add ("yylex ");
Add ("ar5e ");
}});

The first layer of the arc defines an internal Anonymous class (anonymous inner class). The second layer of the arc is actually an instance initialization block (instance initializer block ), this block is executed during internal Anonymous class construction. This block is called "instance initialization block" because it is defined within the instance range of a class. This is different from "static initialzer", because this type of block uses the static keyword before the arc during definition, so it and the class are in the same scope, that is, the class will be executed when it is loaded (for more details, refer to the Java language specification http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.6 ). All methods and variables within the container range can be used in the instance initialization block, but note that the instance initialization block runs before the constructor.

This method applies only to classes that are not final, because the final class cannot create an internal anonymous subclass. Fortunately, this restriction is not applied to the Collection class. Therefore, this method can also be used to initialize any other object, such as a GUI object:

Java code
Add (New jpanel (){{
Setlayout (...);
Setborder (...);
Add (New jlabel (...));
Add (New jspinner (...));
}});
Add (New jpanel (){{
Setlayout (...);
Setborder (...);
Add (New jlabel (...));
Add (New jspinner (...));
}});

In this way, the internal Anonymous class instance is provided with its container reference to the image. If serialization is set, its internal classes will also be serialized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.