Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default values of instance variables and local variables

Tags:

java

I read that Java provides default values to class properties, but not to local variables. Is that correct?

If so, what is the reason behind this? When you are doing something good, why not do it all the way?

like image 842
gameover Avatar asked Jan 15 '10 19:01

gameover


1 Answers

Standard local variables are stored on the stack and aren't actually created until they are initialized. If a local variable isn't used, it doesn't go on the stack. Member variables, however, are allocated in the heap, and thusly have a default placeholder (null reference or default primitive).

like image 70
Michael Krauklis Avatar answered Nov 15 '22 15:11

Michael Krauklis