Why are local variables need to be explicitly assigned, the compiler can not otherwise

1, member variables (global variables) can not be initialized, you can assign it the default value during the preparatory phase of class loading process, but before the local variables need to show given initial value, javac is not deduce not not do it , but did not do so, for a member variable, the value of the order of their assignment and access is uncertain , for member variables can be assigned before a method call can also be carried out after the method call, which is the runtime occurs, the compiler can not determine, jvm to do more appropriate.
2, and for the local variables, its access and value assignment order is determined . This design is a constraint, the greatest extent possible to reduce user mistakes. If local variables can use the default value, may always inadvertently forget an assignment, leading to unpredictable circumstances arise.

Guess you like

Origin www.cnblogs.com/liuqing576598117/p/11165428.html