It is very easy to make an in-process data stream, so itβs very easy to check if a field has been initialized and give warnings if it does not (you can write a simplified decidable algorithm, for example, make sure that all if branches initialize the variable, and if one branch does not work, do not succeeds even if the branch is unavailable).
It is very difficult to perform interprocedural data flow, so it is very difficult to check whether the field of an object has ever been initialized anywhere (you quickly fall into an unsolvable territory for any reasonable approximation).
Thus, Java does the first and gives compile-time errors when it detects uninitialized local variables, but does not do the latter and initializes the object fields by default.
Claudiu
source share