List<String> countries = new ArrayList<String>();
countries.add("Switzerland");
countries.add("France");
countries.add("Germany");
countries.add("Italy");
countries.add("India");
Code language: Java (java)
Above code snippet first creates an object of ArrayList type String and then add one by one countries in it. Now check the Double Brace Initialization of the same code: List<String> countries = new ArrayList<String>() {{
add("India");
add("Switzerland");
add("Italy");
add("France");
add("Germany");
}};
Code language: Java (java)
public class Test {
public Test() {
System.out.println("Constructor called");
}
static {
System.out.println("Static block called");
}
{
System.out.println("Instance initializer called");
}
public static void main(String[] args) {
new Test();
new Test();
}
}
Code language: Java (java)
Static block called Instance initializer called Constructor called Instance initializer called Constructor calledThus the instance initializer code will be called each time a new instance of object is created.
Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Unfortunatelly that code creates anonymous class. Multiple using of such technique will create dozen of anonymous classes, and that could cause problems with perm gen space.
I agree with you Ruslan. This technique is not efficient way of doing this, but I dint knew if such thing exists :-)
I agree with Viral that many Java developers will not be knowing it unless they read the specification. Nice to know that such an option exists.
Interesting to know with clean explanation
Javas Double Brace Instatiation makes it a lot easier to read your source code. For review reasons is that important to you and your colleagues. I think most people only see these advantages if nobody uses them: they would be glad to have them.
A few tips and backgrounds, too: is is about the obstacles and the possibillities with Javas Double Brace Instatiation:
http://bit.ly/endUIi
I hope I could help a little.
Awesome! Never tried such a thing so far. This could be useful in providing easy fixes.
Thanks
Hi,
Anybody please help me , how to send /receive sms from/to java web application/java application.
Nice explanation...Thanks a lot for this article.
[code language="java"]
import java.util.ArrayList;
import java.util.List;
import java.lang.*;
public class MyBasicArrayList {
public static void main(String[] a){
long beginTime = System.currentTimeMillis();
List<String> al = new ArrayList<String>(){{
add("JAVA");
add("C++");
add("PERL");
add("PHP");
}};
long endTime = System.currentTimeMillis();
long difference1 = endTime - beginTime;
System.out.println(al);
long beginTime2 = System.currentTimeMillis();
List<String> a2 = new ArrayList<String>();
a2.add("JAVA");
a2.add("C++");
a2.add("PERL");
a2.add("PHP");
long endTime2 = System.currentTimeMillis();
long difference2 = endTime2 - beginTime2;
System.out.println(al);
//add elements to the ArrayList
if(difference2>difference1){
System.out.println("{{}} Wins");}
else{
System.out.println("{{}} Looses");}
}
}
[/code]
I tried this program and Guess what {{}} Looses..... n times So I appreciate your effort but this is lil slower think..kindly revert If I am wrong.
nice explanation and useful tutorial , thanks ;)