aspose file tools
The moose likes Beginning Java and the fly likes Instance Block vs Constructor! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance Block vs Constructor!" Watch "Instance Block vs Constructor!" New topic

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! in above example wen value is intialized through instance block 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: 1747

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

Joined: Oct 13, 2005
Posts: 40961
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!