Why must local variables have initial values?

Fields are automatically initialized to the logical zero for the type; this is implicit. Variables must obey “definite assignment”, so must be assigned before they can be read.

ECMA 334v4

§17.4.4 Field initialization

The initial value of a field, whether
it be a static field or an instance
field, is the default value (§12.2) of
the field’s type. It is not possible
to observe the value of a field before
this default initialization has
occurred, and a field is thus never
“uninitialized”.

and

§12. Variables


A variable shall be definitely assigned (§12.3) before its
value can be obtained.

Leave a Comment

tech