java - why data members are initialized with default values whereas local variables are not? -


can 1 time reason behind assigning default values data members , not local variables ? there specific reason ?

example :

class { b b;  public void f(){ int a; } 

in above class b initialized null, compiler throws , error saying variable inside f() not initialized.

i (as always) practical reason. can initialize object @ 1 point , use time later - have have mechanism ensure initialized default value in case cannot set them immediately.

local object on other hand used after declared - can safely assume programmer able initiate them target value, , should encouraged.

bottom line - practical reasons, encouraging practices prevent errors, nothing technical limitations.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -