What is the alternative to a non-static initialization block?

In my projects, there was a developer who liked the non-static initialization block.

What is an alternative , and what is the disadvantage of this alternative? I would suggest: initialize values ​​in constructor ?

Why should we use a block without initialization? As I understand it, the “initialization block” is used to set values ​​when creating an instance of the class. Is then a constructor not enough?

public class BlockTest { String test = new String(); //Non-static initialization block { test = "testString"; } } 

This block confuses me and leads to a decrease in readability. Thanks for your reply!

+6
source share
3 answers

First of all, it makes no sense to initialize the test for a new String (), since the initialization block immediately assigns it something else. Anyway...

The ad indicates one alternative:

 public class BlockTest { String test = "testString"; } 

Other in constructor:

 public class BlockTest { String test; public BlockTest () { test = "testString"; } } 

These are two basic, general options.

For the initialization block, two main applications are used. The first is for anonymous classes that must execute some logic during initialization:

 new BaseClass () { List<String> strings = new ArrayList<String>(); { strings.add("first"); strings.add("second"); } } 

The second is for normal initialization, which must be performed before each constructor:

 public class MediocreExample { List<String> strings = new ArrayList<String>(); { strings.add("first"); strings.add("second"); } public MediocreExample () { ... } public MediocreExample (int parameter) { ... } } 

However, in both cases there are alternatives that do not use the initialization block:

 new BaseClass () { List<String> strings = createInitialList(); private List<String> createInitialList () { List<String> a = new ArrayList<String>(); a.add("first"); a.add("second"); return a; } } 

and

 public class MediocreExample { List<String> strings; private void initialize () { strings = new List<String>(); strings.add("first"); strings.add("second"); } public MediocreExample () { initialize(); ... } public MediocreExample (int parameter) { initialize(); ... } } 

There are many ways to do this, use the most appropriate method, and provide the most clear and convenient code.

+10
source

The compiler inserts a non-static init block into each constructor. The code needed to initialize instance fields is also inserted into each constructor. it

 class Test1 { int x = 1; { x = 2; } Test1() { x = 3; } } 

compiles to the same file as this one

 class Test1 { int x; Test1() { x = 1; x = 2; x = 3; } } 
+6
source

the initialization block has no alternative, in fact it is an alternative to the constructor.

 public TestClass { TestClass() { } } 

this is useful in the case of an anonymous class, because you cannot have a constructor, because the reason is quite simple, that you do not have a name for the class, then you cannot have a constructor, otherwise you name it.

 new MyClass(){ //its an anonymous class, you can't use constructor here { } } 

however, you can initialize the inline variable with your declaration, for example

 public TestClass { String test = "value"; } 

but this is not an alternative, because you cannot perform any operation (for example, arithmetic or string operations) in this way, but you can do in the initialization block

 public TestClass { String test = "value"; test = test + " not found"//compiler error { test = test + " not found" // valid } } 
+2
source

Source: https://habr.com/ru/post/951194/


All Articles