wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance Block vs Constructor!" Watch "Instance Block vs Constructor!" New topic
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: 1659
    
  11

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: 37900
    
  22
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).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Instance Block vs Constructor!
 
Similar Threads
Initializers
Abstract Class doubt
enums do not allow access to static members within constructors and instance init blocks
Question regarding Init block
about forward refrencing