diff options
| author | Levi Bostian <levi.bostian@gmail.com> | 2015-10-06 09:35:17 -0500 | 
|---|---|---|
| committer | Levi Bostian <levi.bostian@gmail.com> | 2015-10-06 09:35:17 -0500 | 
| commit | c7fd8814af32822109949b6381156844c32422c0 (patch) | |
| tree | 4950a25c85f62169f49ccf0d9d94cfa6ea6adb51 /java.html.markdown | |
| parent | e0f81c1a9f6329d785ecc558f1a380c24c26809d (diff) | |
| parent | 8b456426407997fdbb8a0befb4181a513e585231 (diff) | |
Merge pull request #1357 from perlilja/java/en
[java/en] Added description for Double Brace Initialization
Diffstat (limited to 'java.html.markdown')
| -rw-r--r-- | java.html.markdown | 27 | 
1 files changed, 27 insertions, 0 deletions
| diff --git a/java.html.markdown b/java.html.markdown index e4234a39..b4757962 100644 --- a/java.html.markdown +++ b/java.html.markdown @@ -325,6 +325,33 @@ public class LearnJava {          // toString returns this Object's string representation.          System.out.println("trek info: " + trek.toString()); +         +        // Double Brace Initialization +        // The Java Language has no syntax for how to create static Collections +        // in an easy way. Usually you end up in the following way: +         +        private static final Set<String> COUNTRIES = new HashSet<String>(); +        static { +	       validCodes.add("DENMARK"); +	       validCodes.add("SWEDEN"); +	       validCodes.add("FINLAND"); +        } +         +        // But there's a nifty way to achive the same thing in an +        // easier way, buy using something that is called Double Brace +        // Initialization. +         +        private static final Set<String> COUNTRIES = HashSet<String>() {{ +            add("DENMARK"); +            add("SWEDEN"); +            add("FINLAND");             +        }} +         +        // The first brace is creating an new AnonymousInnerClass and the  +        // second one declares and instance initializer block. This block +        // is called with the anonymous inner class is created. +        // This does not only work for Collections, it works for all +        // non-final classes.      } // End main method  } // End LearnJava class | 
