This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes need for constructors in this case 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "need for constructors in this case" Watch "need for constructors in this case" New topic
Author

need for constructors in this case

saurabhthard aggarwal
Greenhorn

Joined: Nov 20, 2008
Posts: 22
Why do we need constructors when we can initialize variables in INSTANCE INTIALIZATION block
Is it just because we can over load constructors and not overload INSTANCE INTIALIZATION block .
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



All statements inside INSTANCE INTIALIZATION block are finally copied to all available constructors , so no difference i see.
So its common initialisation block which is not " specific to any constructor "
saurabhthard aggarwal
Greenhorn

Joined: Nov 20, 2008
Posts: 22
Yes it is right that instance blocks are copied inside the construtor, but my question is that why do we require constructor when we can give initial value inside instance initialisation block
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874



Yes it is right that instance blocks are copied inside the construtor, but my question is that why do we require constructor when we can give initial value inside instance initialisation block


Initialization block s are meant intialise things and they dont directly replace constructors in any way. What would happen if call new Obj() ? , constructor is called and initialisation block is called impliictly by constrcutor. HTH.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
This is quite simple.

Try to construct an Integer with value 10, and then another constructor with value 20, without using a constructor.

Instance initialization blocks usually contain code that should be shared by all constructors, like setting part of the initial state of objects that should always be the same.


All code in my posts, unless a source is explicitly mentioned, is my own.
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
That is not only the reason....what about the access....
Constructor can be replaced by Intitialisation block if you have one constructor and of default access....
If you have more than one constructor and they all doing some common thing then you should move those common thing in to intialisation block...
By the way initialisation blocks are not substitute of constructor.........they are less powerful than constrotor.....


Thanks
Vijay Saraf.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
vijay saraf wrote:That is not only the reason....what about the access....
Constructor can be replaced by Intitialisation block if you have one constructor and of default access....
If you have more than one constructor and they all doing some common thing then you should move those common thing in to intialisation block...
By the way initialisation blocks are not substitute of constructor.........they are less powerful than constrotor.....

I never said it was the only reason, the OP was asking why constructors can't be replaced with instance initialization blocks, and I just gave a reason why they can't.

Also, what you said is not correct (a constructor can't be replaced by initialization blocks when you only have a constructor of default access.) If you want to be able to create objects with different states at instantiation time you need to have a constructor to do that. I really don't know what default access has to do with this either, except to say that if a constructor is public for a class that is not, you still can't construct instances of the class using the class constructor directly outside its package (because the class is not accessible outside its package.) As a matter of fact, you can't even declare reference variables of that type.
Praveen mourya Kumar
Greenhorn

Joined: Oct 14, 2008
Posts: 16
saurabhthard aggarwal wrote:Why do we need constructors when we can initialize variables in INSTANCE INTIALIZATION block
Is it just because we can over load constructors and not overload INSTANCE INTIALIZATION block .


Hi Sarurabh,

It is not all about the initialization of data members. Suppose you want to sell your java package for a limited time period. so you declare that constructor as private and end user can use until he is paying the package amount.

SCJP 1.6..
vijay saraf
Ranch Hand

Joined: Jan 08, 2005
Posts: 141
Ruben, i just wanted to light up the point that constructor can have access modifiers...while initialization block can't...
as saurabhthard as asked :
Is it just because we can over load constructors and not overload INSTANCE INTIALIZATION block .
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
vijay saraf wrote:Ruben, i just wanted to light up the point that constructor can have access modifiers...while initialization block can't...
as saurabhthard as asked :
Is it just because we can over load constructors and not overload INSTANCE INTIALIZATION block .

I understand what you mean, Vijay.
 
 
subject: need for constructors in this case
 
Similar Threads
Initialization sequence
break label
return statement from Constructor
initializer block
super.init(config) in servlets