What is the use of non-static block in java?

Possible duplicate:
How does an instance initializer differ from a constructor?

When all the necessary work can be done inside the constructor, why do we still need a non-static block in Java?

EDIT: What about regular classes for which non-static blocks are run every time before the constructor?

+7
source share
2 answers

In addition to @Bohemian answer.

If you have multiple constructors, the intialiser block avoids duplication.

public class A { final Map<String, String> map = new HashMap<String, String>(); { // put things in map. } final int number; { int number; try { number = throwsAnException(); } catch (Exception e) { number = 5; } this.number = number; } public A() { /* constructor 1 */ } public A(int i) { /* constructor 2 */ } } 

What about regular classes for which non-static blocks are run every time before the constructor?

Technically order

  • super constructor is always called first
  • all initializer blocks in order of appearance.
  • constructor code

In fact, all this code is in byte code for each constructor, so there is nothing before or after the constructor.


Before any confusion arises regarding the initialization order

 public class Main extends SuperClass { { System.out.println("Initialiser block before constructor"); } Main() { System.out.println("Main constructor"); } { System.out.println("Initialiser block after constructor"); } public static void main(String... args) { new Main() {{ System.out.println("Anonymous initalizer block"); }}; } } class SuperClass { SuperClass() { System.out.println("SuperClass constructor"); } } 

prints

 SuperClass constructor Initialiser block before constructor Initialiser block after constructor Main constructor Anonymous initalizer block 
+12
source

You can use it with an anonymous class:

 new MyClass() { { // do something extra on construction (after the constructor executes) } } 

I find this particularly useful for initializing β€œsearch” maps (i.e. fixed content) in place:

 Map<String, String> map = new HashMap<String, String>() { { put("foo", "bar"); put("one", "two"); // etc } }; 

FYI, this is sometimes (weakly) called "double-bracket initialization" when in fact it just uses an initializer block.

Although such an anonymous class is technically a subclass, the beauty of this is shown when comparing using this method with the more traditional one when creating an unmodifiable map:

Compare this simple single-line layer that combines data and purpose:

 private static final Map<String, String> map = Collections.unmodifiableMap( new HashMap<String, String>() {{ put("foo", "bar"); put("one", "two"); // etc }}); 

With this mess, which should create a separate object because of final , allowing only one assignment:

 private static final Map<String, String> map; static { Map<String, String> tempMap = new HashMap<String, String>(); tempMap.put("foo", "bar"); tempMap.put("one", "two"); // etc map = Collections.unmodifiableMap(tempMap); } 

Also note that with the mess version these two statements should not be contiguous, so it becomes less obvious what the contents of an unmodifiable map are.

+6
source

All Articles