Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Advanced
Sep 14, 2009

Achieve Syntax Brevity with Anonymous Classes and Instance Initializers

The first listing here, which may look more familiar, is considerably longer than the second listing, even though they're essentially equivalent.

Listing 1.
List friends = new ArrayList();
friends.add("Ben");
friends.add("Kiran");
friends.add("....");

You can replace all the code in Listing 1 with an anonymous class that uses an instance initializer as shown in Listing 2.

Listing 2.
new ArrayList(){{add("Ben");add("Kiran");add("....");}};
Abdul Salam
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap