constructor style?

Discussion in 'Java' started by Duane Evenson, May 15, 2006.

  1. A quick question about constructor style:
    Which do you prefer, independently built constructors or nested
    constructors?

    eg.
    // Eclipse automatically generated style
    AClass() {
    }
    AClass(String str) {
    this.str = str;
    }
    AClass(String str, int num) {
    this.str = str;
    this.num = num;
    }

    or
    // "Elements of Java Style" recommended style
    AClass(String str, int num) {
    this.string = string;
    this.num = num;
    }
    AClass(String str) {
    this(str, DEFAULT_NUM);
    }
    AClass() {
    this("", DEFAULT_NUM);
    }
    Duane Evenson, May 15, 2006
    #1
    1. Advertising

  2. On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
    <> wrote:
    >A quick question about constructor style:
    >Which do you prefer, independently built constructors or nested
    >constructors?


    It really depends on the amount of logic in the constructors, but, as
    a general preference, nested constructors - no code duplication.

    Domchi

    --
    Ouroboros ltd. - http://www.ouroboros.hr
    Antispam: to reply, remove extra monkey from reply-to address.
    Domagoj Klepac, May 15, 2006
    #2
    1. Advertising

  3. Domagoj Klepac wrote:
    > On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
    > <> wrote:
    >> A quick question about constructor style:
    >> Which do you prefer, independently built constructors or nested
    >> constructors?

    >
    > It really depends on the amount of logic in the constructors, but, as
    > a general preference, nested constructors - no code duplication.


    Ditto.
    Jeffrey Schwab, May 15, 2006
    #3
  4. Duane Evenson schrieb:
    > A quick question about constructor style:
    > Which do you prefer, independently built constructors or nested
    > constructors?
    >
    > eg.
    > // Eclipse automatically generated style
    > AClass() {
    > }
    > AClass(String str) {
    > this.str = str;
    > }
    > AClass(String str, int num) {
    > this.str = str;
    > this.num = num;
    > }
    >
    > or
    > // "Elements of Java Style" recommended style
    > AClass(String str, int num) {
    > this.string = string;
    > this.num = num;
    > }
    > AClass(String str) {
    > this(str, DEFAULT_NUM);
    > }
    > AClass() {
    > this("", DEFAULT_NUM);
    > }


    I'd prefer the latter one. Every constructor finally leads to the "most
    flexible" one. If you have to change anything, you have to do it only
    once and not - as in this example - thrice.

    It's the same with methods: normally you would implement
    List#add(Object) as

    <code>
    public void add(Object obj) {
    this.add(this.size(), obj);
    }
    </code>

    and not do the implementation twice for List#add(Object) and
    List#add(int, Object), which are technically the same.

    Tobi
    =?ISO-8859-1?Q?Tobias_Schr=F6er?=, May 15, 2006
    #4
  5. On Mon, 15 May 2006 14:12:55 +0000, Jeffrey Schwab wrote:

    > Domagoj Klepac wrote:
    >> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
    >> <> wrote:
    >>> A quick question about constructor style:
    >>> Which do you prefer, independently built constructors or nested
    >>> constructors?

    >>
    >> It really depends on the amount of logic in the constructors, but, as
    >> a general preference, nested constructors - no code duplication.

    >
    > Ditto.


    thanks
    Duane Evenson, May 15, 2006
    #5
  6. Jeffrey Schwab wrote:
    > Domagoj Klepac wrote:
    >> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
    >> <> wrote:
    >>> A quick question about constructor style:
    >>> Which do you prefer, independently built constructors or nested
    >>> constructors?

    >>
    >> It really depends on the amount of logic in the constructors, but, as
    >> a general preference, nested constructors - no code duplication.

    >
    > Ditto.


    +1

    robert
    Robert Klemme, May 15, 2006
    #6
    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. Giulio
    Replies:
    9
    Views:
    1,030
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,321
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,806
    David Harmon
    Jan 23, 2004
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,204
  5. Ken Varn
    Replies:
    0
    Views:
    430
    Ken Varn
    Apr 26, 2004
Loading...

Share This Page