Default values ​​for local variables?

In Java , why class variables get an initialized default value. But local variables are not.?

Can anyone explain?

+6
source share
1 answer

Local variables are not initialized to default values, as in the case of class variables. The same goes for primitives and object reference.

JavaDoc (section 4.12.5. Initial values ​​of variables):

 A local variable must be explicitly given a value before it is used, by either initialization or assignment, in a way that can be verified using the rules for definite assignment. 
-2
source

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


All Articles