Cascading through the constructors

J

Jon Skeet [C# MVP]

K Viltersten said:
As a human, i'm lazy, so if i need to
write a class with three constructors,
i'd like to minimize the work. I'm used
to two different approaches and i'd
like to get an opinion on which is to
be most preferred.

// 1. Constructors calling each other.
class C {
C () {this (5);}
C (int i) {this (5, 3);}
C (int i, int j) {} }

// 2. Constructors calling a common method.
class C {
C () {common (5, 3);}
C (int i) {common (i, 3);}
C (int i, int j) {common (i, j);}
void common (int i, int j) {} }

My applogies for the ugly code. I tried to
keep it short.

I personally usually use option 1 - although I vary between a "long"
constructor chain (where the parameterless calls the single parameter,
which calls the double parameter) and "short" constructor chains (where
every constructor calls the most complicated one).
 
K

K Viltersten

As a human, i'm lazy, so if i need to
write a class with three constructors,
i'd like to minimize the work. I'm used
to two different approaches and i'd
like to get an opinion on which is to
be most preferred.

// 1. Constructors calling each other.
class C {
C () {this (5);}
C (int i) {this (5, 3);}
C (int i, int j) {} }

// 2. Constructors calling a common method.
class C {
C () {common (5, 3);}
C (int i) {common (i, 3);}
C (int i, int j) {common (i, j);}
void common (int i, int j) {} }

My applogies for the ugly code. I tried to
keep it short.
 
K

K Viltersten

Which is to be most preferred?
I personally usually use option 1 - although
I vary between a "long" constructor chain
(where the parameterless calls the single
parameter, which calls the double parameter)
and "short" constructor chains (where every
constructor calls the most complicated one).

Thanks!

Just one more thing. I don't seem to be able
to compile

C () {this (5, 3);}

as the compiler nags about "method name
expected". I expected the same syntax as in
Java, perhaps foolishly... Help?

I suspect i could use the syntax:

C () : this (5, 3) {}

but i don't want to risk it. I want to make
sure that the LAST thing done in the simple
constructor is calling the complicated one,
not the first thing.
 
J

Jon Skeet [C# MVP]

K Viltersten said:
Thanks!

Just one more thing. I don't seem to be able
to compile

C () {this (5, 3);}

as the compiler nags about "method name
expected". I expected the same syntax as in
Java, perhaps foolishly... Help?

You've got it here:
I suspect i could use the syntax:

C () : this (5, 3) {}

but i don't want to risk it. I want to make
sure that the LAST thing done in the simple
constructor is calling the complicated one,
not the first thing.

No, it's just like Java in that respect (though not in the syntax) -
the chained constructor call is always the first thing to be executed.
 
K

K Viltersten

Just one more thing. I don't seem to be able
You've got it here:


Are you saying that i can't call a constructor
in a class C from INSIDE an other constructor
in the same class C?! That's surprising...
 
J

Jon Skeet [C# MVP]

K Viltersten said:
Are you saying that i can't call a constructor
in a class C from INSIDE an other constructor
in the same class C?! That's surprising...

Well, bear in mind that every constructor is meant to *eventually* call
a base class constructor (potentially though chaining) - and that is
designed to happen before any other code in your class is called (aside
from variable initializers).

As I say, the same restriction is in Java. I don't think the CLR
requires it, admittedly.
 
K

K Viltersten

Are you saying that i can't call a constructor
Well, bear in mind that every constructor is meant
to *eventually* call a base class constructor
(potentially though chaining) - and that is
designed to happen before any other code in your
class is called (aside from variable initializers).

As I say, the same restriction is in Java. I don't
think the CLR requires it, admittedly.

It's been a while i've been working with Java but i
remember that the construction was as follows.

class J {
public J () {this(5);}
public J (int i) {} }

I.e., the call to a fellow constructor has been
placed INSIDE the body and one could, as far my
memory serves me, execute statements BEFORE the
call to the constructor.

The call to the base, however, wasn't so freely
placeable. It had to come first in the constructor.
 
A

Arne Vajhøj

K said:
It's been a while i've been working with Java but i
remember that the construction was as follows.

class J {
public J () {this(5);}
public J (int i) {} }

I.e., the call to a fellow constructor has been
placed INSIDE the body and one could, as far my
memory serves me, execute statements BEFORE the
call to the constructor.

The first is correct. You call this inside {}.

The second is not correct. You can not call
other statements before this.

Arne
 
J

Jon Skeet [C# MVP]

K Viltersten said:
It's been a while i've been working with Java but i
remember that the construction was as follows.

class J {
public J () {this(5);}
public J (int i) {} }

I.e., the call to a fellow constructor has been
placed INSIDE the body
Correct.

and one could, as far my
memory serves me, execute statements BEFORE the
call to the constructor.

Incorrect. From the spec
(http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html)

<quote>
The first statement of a constructor body may be an explicit invocation
of another constructor of the same class or of the direct superclass (§
8.8.7.1).

ConstructorBody:
{ ExplicitConstructorInvocationopt BlockStatementsopt }

</quote>
 
K

K Viltersten

It's been a while i've been working with Java but i
The first is correct. You call this inside {}.

The second is not correct. You can not call
other statements before this.


Then i guess i was mistaken. It's been a while
since i've touched anything bean-related, so i'll
stand corrected.

In that case, thank you both for the correction.
 

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

Top