I did avoid the init block because it hurts readability.
For example:
class Foo {
Bar someObj;
{
someObj = new Bar();
}
Foo() {}
}
Now, think about it—when exactly does the init block run? Does it execute before every constructor? What happens if you add another constructor?
You might know the answer, but the real question is: Will the next person reading your code know?
Instead, just initialize the variable directly:
class Foo {
Bar someObj = new Bar();
}
Now, it’s clear and predictable—no matter how many constructors you add, everyone knows someObj gets initialized when the object is created.
Even better? Use a constructor:
class Foo {
Bar someObj;
Foo() {
someObj = new Bar();
}
}
This makes it explicit when and how someObj is initialized, and if needed, you can change or pass different values.