Calling own-constructors inside another constructor

E

EdUarDo

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.
 
T

Thomas Weidenfeller

EdUarDo 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.

/Thomas
 
R

Roedy Green

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.
 
E

EdUarDo

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("")?
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top