No. Instance variables of type integer are initialized to 0, that's true. But if you set their value in the init block, they won't be reset in the constructor. That would be annoying to have to reinitialize them again in the constructor, wouldn't it ?
The main role of the constructor is to allow to instantiate a class, not to reset instance variables.
Arun C. Giridharan
Joined: Jul 11, 2010
AS i know instantiate means bringing code or blueprint or class into existence ...right.....so each must have a value ....in case if i don't have Init block these integer specimen i and j would be set to 0.....wat happens wen i specify Init block ...i really doubt on tht ....
losing it's property or giving state(value to instance variable)to the Object....
Why would the constructor initialize variables? A constructor will just do whatever is in the constructor code--in this case, nothing. What if you had the following?Should the constructor wipe out the value of a? No.
Init block are just a way of saying - 'hey put these bunch of statements in each of my constructors'. The idea is to put in all initializing code in one place instead of in each and every one of the constructors.
Init block code is usually in-lined to the beginning of each constructor.
If you de-compile your class, you would see something like this