My projects had some developer who loved a non-static initialization block.
What is the alternative to this and what is the downside of this alternative? I would guess: initialize the values in the constructor ?
Why should we use a non-initialization block? As far as I understood the "initialization block" is used to set values when instantiating the class. Is a constructor then not sufficient?
public class BlockTest {
String test = new String();
//Non-static initialization block
{
test = "testString";
}
}
This block confuses me and lead to reduced readability. Thanks for your response!
Instance variables are initialized using initialization blocks. These blocks are executed when the class object is created and before the invocation of the class constructor. Also, it is not necessary to have initialization blocks in the class.
The Non-static blocks are class level blocks which do not have any prototype. The need for a non-static block is to execute any logic whenever an object is created irrespective of the constructor. The Non-static blocks are automatically called by the JVM for every object creation in the java stack area.
First of all, there are two types of initialization blocks: instance initialization blocks, and. static initialization blocks.
First of all, it doesn't make sense to initialize test to a new String() there, since the initialization block immediately assigns it to something else. Anyways...
One alternative is initializing at the declaration:
public class BlockTest {
String test = "testString";
}
Another is in the constructor:
public class BlockTest {
String test;
public BlockTest () {
test = "testString";
}
}
Those are the two main, common options.
There are two main uses for an initialization block. The first is for anonymous classes that have to perform some logic during initialization:
new BaseClass () {
List<String> strings = new ArrayList<String>();
{
strings.add("first");
strings.add("second");
}
}
The second is for common initialization that must happen before every 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 these things, use the way that is most appropriate and provides the clearest and most easily maintainable code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With