This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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 Java 8 in Action this week in the Java 8 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: 1615
    
  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: 36578
    
  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
Question regarding Init block
about forward refrencing
Initializers
enums do not allow access to static members within constructors and instance init blocks