| Author |
Instance Block vs Constructor!
|
Arun C. Giridharan
Ranch Hand
Joined: Jul 11, 2010
Posts: 96
|
|
Can anyone say the difference between Instance block and constructor.....they bothr use to intialize .....see the following code
....actually constructor is used to instialize the value 0 ...for instance variables right!...as in above example wen value is intialized through instance block ...in constructor those must (instance variable)...must again set back to 0...right! .....javascript:emoticon(' ');
EDIT: Code Tags - JD
|
 |
Janeice DelVecchio
Saloon Keeper
Joined: Sep 14, 2009
Posts: 1612
|
|
Welcome to JavaRanch!
Try to remember to use code tags when you post.... it will help folks to read what you've posted
Since you're new, and I'm incredibly handy, I'll go ahead and add them for you
|
When you do things right, people won't be sure you've done anything at all.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
|
By "instance block" I presume you mean "instance initializer". A constructor can have parameters passed to it, and is probably a better way to initialise the values so as to establish the class invariant. Have look at the Java™ Language specification about initializers and constructors (1) and maybe (2).
|
 |
 |
|
|
subject: Instance Block vs Constructor!
|
|
|