File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 1613
    
  10

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: 36508
    
  16
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Instance Block vs Constructor!
 
Similar Threads
Abstract Class doubt
about forward refrencing
enums do not allow access to static members within constructors and instance init blocks
Initializers
Question regarding Init block