Calling own-constructors inside another constructor

Discussion in 'Java' started by EdUarDo, Dec 21, 2005.

  1. EdUarDo

    EdUarDo Guest

    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.
    EdUarDo, Dec 21, 2005
    #1
    1. Advertising

  2. 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/
    Thomas Weidenfeller, Dec 21, 2005
    #2
    1. Advertising

  3. EdUarDo

    Roedy Green Guest

    On Wed, 21 Dec 2005 10:45:51 +0100, Thomas Weidenfeller
    <> 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.
    Roedy Green, Dec 21, 2005
    #3
  4. EdUarDo

    EdUarDo Guest

    > 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("")?
    EdUarDo, Dec 21, 2005
    #4
  5. EdUarDo

    Roedy Green Guest

    On Wed, 21 Dec 2005 11:31:37 +0100, EdUarDo
    <> 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.
    Roedy Green, Dec 21, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    1
    Views:
    762
  2. Asfand Yar Qazi
    Replies:
    6
    Views:
    15,607
    jeffc
    May 17, 2004
  3. Neroku
    Replies:
    8
    Views:
    1,178
    Tor Iver Wilhelmsen
    Nov 18, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,203
  5. Alf P. Steinbach
    Replies:
    4
    Views:
    397
    James Kanze
    Nov 30, 2007
Loading...

Share This Page