-2

instance and static variables will be initialized with a default values if we don't initialize with our own values but it doesn't happen with local variables. Why they designed like so ?

John
  • 59
  • 6

1 Answers1

3

The reason we have default values at all is that it's was decided it was too complicated to determine if a variable is initialised before it is used (unless it's final) This is because you could call methods in any order.

For local variable it can determine whether you have used a variable before you gave it a value, so this check prevents you using uninitialised values to avoid errors in your code.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130