Calling own-constructors inside another constructor  
Author Message
EdUarDo





PostPosted: 2005-12-21 17:10:00 Top

java-programmer, Calling own-constructors inside another constructor Hi all,

Inspecting some pieces of code from someone I found things like this:

class A {
private String id = "";

public A() {
super();
}

public A(String id) {
this.id = id;
}
}

class B extends A {
public B() {
this(""); <-------------------- HERE!
}

public B(String id) {
super(id);
}
}


Inside constructor B() it's calling a constructor. Could this cause some collateral or strange problems?
I know it's nonsense because id attribute it's being initialized by default to "", but I'd like to know if
also is a problem.
 
Thomas Weidenfeller





PostPosted: 2005-12-21 17:46:00 Top

java-programmer >> Calling own-constructors inside another constructor EdUarDo wrote:
> class B extends A {
> public B() {
> this(""); <-------------------- HERE!
> }
>
> public B(String id) {
> super(id);
> }
> }
>
>
> Inside constructor B() it's calling a constructor. Could this cause some
> collateral or strange problems?

No, it is a common idiom. If you don't use this(), Java will silently
insert a call to the no-arg superclass constructor. Instead of geting
that default behavior, the usage of this() allows to specify some more
clever bahaviour.

You probably think about calling ordinary methods from a constructor.
That, if not properly done, can cause problems.

/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
 
Roedy Green





PostPosted: 2005-12-21 18:22:00 Top

java-programmer >> Calling own-constructors inside another constructor On Wed, 21 Dec 2005 10:45:51 +0100, Thomas Weidenfeller
<email***@***.com> wrote, quoted or indirectly quoted someone
who said :

>> class B extends A {
>> public B() {
>> this(""); <-------------------- HERE!
>> }
>>
>> public B(String id) {
>> super(id);
>> }
>> }
>>
>>
>> Inside constructor B() it's calling a constructor. Could this cause some
>> collateral or strange problems?
>
>No, it is a common idiom. If you don't use this(), Java will silently
>insert a call to the no-arg superclass constructor. Instead of geting
>that default behavior, the usage of this() allows to specify some more
>clever bahaviour.
>
>You probably think about calling ordinary methods from a constructor.
>That, if not properly done, can cause problems.

If you have a B(String id) constructor, Java WON'T create the default
no-arg constructor for you. If you don't write a no-arg constrictor,
and go ahead and attempt to use it anyway, Java will give you a
compile time error. Java only creates the no-arg constructor if you
have no constructors defined.

A constructor calling another with this(...) is common to provide
default parameters.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
EdUarDo





PostPosted: 2005-12-21 18:32:00 Top

java-programmer >> Calling own-constructors inside another constructor > A constructor calling another with this(...) is common to provide
> default parameters.

But since the attribute is already initialized to "" in the superclass, why don't use super() instead of this("")?
 
 
Roedy Green





PostPosted: 2005-12-21 20:23:00 Top

java-programmer >> Calling own-constructors inside another constructor On Wed, 21 Dec 2005 11:31:37 +0100, EdUarDo
<email***@***.com> wrote, quoted or indirectly
quoted someone who said :

>> A constructor calling another with this(...) is common to provide
>> default parameters.
>
>But since the attribute is already initialized to "" in the superclass, why don't use super() instead of this("")?

Because this("") is more encapsulated. It depends only on the
behaviour of the current class, not the superclass. You want to
create as few interdependencies between classes as possible and you
don't want to write code that depends on undocumented current
behaviour. That could easily change and your code would mysteriously
stop working.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.