Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #473 > unrolled thread

Constructors order of invocation

Started byMerciadri Luca <Luca.Merciadri@student.ulg.ac.be>
First post2011-04-02 21:59 +0200
Last post2011-04-03 14:16 -0400
Articles 7 — 5 participants

Back to article view | Back to comp.lang.java.help


Contents

  Constructors order of invocation Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-02 21:59 +0200
    Re: Constructors order of invocation Lew <noone@lewscanon.com> - 2011-04-02 17:20 -0400
    Re: Constructors order of invocation Roedy Green <see_website@mindprod.com.invalid> - 2011-04-02 21:33 -0700
    Re: Constructors order of invocation Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> - 2011-04-03 11:07 +0200
    Re: Constructors order of invocation Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-03 08:24 -0400
      Re: Constructors order of invocation Patricia Shanahan <pats@acm.org> - 2011-04-03 08:55 -0700
        Re: Constructors order of invocation Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-03 14:16 -0400

#473 — Constructors order of invocation

FromMerciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Date2011-04-02 21:59 +0200
SubjectConstructors order of invocation
Message-ID<878vvsqvat.fsf@merciadriluca-station.MERCIADRILUCA>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Let us consider three classes: S3, S2 and S1. S3 is defined as a
direct subclass of S2, which is defined as a direct subclass of S1. S1
has a parent class, but S1's declaration does not precise its parent
class.

The declarations of classes S1, S2 and S3 might define 0, 1, or more
constructors.

1. If S3 is instanciated, which constructors will be executed, and in
which order? I thought that the one of S3 might be executed first (if
any), and then the one of S2 (if any), and then the one of S1 (if
any).

2. How can one modify the number and the order of the executed
constructors (assuming one modifies S3).

Thanks.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Don't count your chickens before they're hatched.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2XgAoACgkQM0LLzLt8MhxIqwCfVykQttqfr2Hq+6XSyF/woagp
sG4AnRBB1GT3Ra902AhxY65PH83vujbc
=EiLA
-----END PGP SIGNATURE-----

[toc] | [next] | [standalone]


#478

FromLew <noone@lewscanon.com>
Date2011-04-02 17:20 -0400
Message-ID<in83vc$ekt$1@news.albasani.net>
In reply to#473
Merciadri Luca wrote:
> Let us consider three classes: S3, S2 and S1. S3 is defined as a
> direct subclass of S2, which is defined as a direct subclass of S1. S1
> has a parent class, but S1's declaration does not precise [?] its parent
> class.

What does "precise" mean here?

> The declarations of classes S1, S2 and S3 might define 0, 1, or more
> constructors.

There will always be a constructor defined.  If the source does not 
*explicitly* define one, it *implicitly* defines a no-argument ("no-arg") 
constructor with no content in the body.

This implicit no-arg constructor is called a "default constructor".

Thus, every class will always have a constructor defined, either by the source 
or by the compiler.

> 1. If S3 is instanciated [sic], which constructors will be executed, and in
> which order? I thought that the one of S3 might be executed first (if
> any), and then the one of S2 (if any), and then the one of S1 (if
> any).

Did you think of looking up this information instead of guessing with no basis?

RTFM.
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9>

Note that *EVERY* class has a constructor, and perhaps more than one.

> 2. How can one modify the number and the order of the executed
> constructors (assuming one modifies S3).

What have you learned so far that affects construction?

List those things, then we can examine whether they affect construction.

Have you read the tutorials on this topic?

<http://download.oracle.com/javase/tutorial/java/javaOO/constructors.html>
ff.
<http://download.oracle.com/javase/tutorial/java/javaOO/objectcreation.html>
<http://download.oracle.com/javase/tutorial/java/javaOO/initial.html>
<http://download.oracle.com/javase/tutorial/java/IandI/super.html>

These are not rhetorical questions.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#486

FromRoedy Green <see_website@mindprod.com.invalid>
Date2011-04-02 21:33 -0700
Message-ID<71ufp6dtm7o61cm9utqgtnboj0hku2p584@4ax.com>
In reply to#473
On Sat, 02 Apr 2011 21:59:06 +0200, Merciadri Luca
<Luca.Merciadri@student.ulg.ac.be> wrote, quoted or indirectly quoted
someone who said :

>Let us consider three classes: S3, S2 and S1. S3 is defined as a
>direct subclass of S2, which is defined as a direct subclass of S1. S1
>has a parent class, but S1's declaration does not precise its parent
>class.

The order of initialisation is intricate, but logical.  See
http://mindprod.com/jgloss/initialisation.html
and 
http://mindprod.com/jgloss/constructor.html

The main thing you have to watch out for is using virtual methods in
constructors that Java will let use variables before they are
initialised.
-- 
Roedy Green Canadian Mind Products
http://mindprod.com
Doing what the user expects with respect to navigation is absurdly important for user satisfaction.
~ anonymous Google Android developer

[toc] | [prev] | [next] | [standalone]


#490

FromMerciadri Luca <Luca.Merciadri@student.ulg.ac.be>
Date2011-04-03 11:07 +0200
Message-ID<87y63rbt4p.fsf@merciadriluca-station.MERCIADRILUCA>
In reply to#473
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> writes:

> Let us consider three classes: S3, S2 and S1. S3 is defined as a
> direct subclass of S2, which is defined as a direct subclass of S1. S1
> has a parent class, but S1's declaration does not precise its parent
> class.
>
> The declarations of classes S1, S2 and S3 might define 0, 1, or more
> constructors.
>
> 1. If S3 is instanciated, which constructors will be executed, and in
> which order? I thought that the one of S3 might be executed first (if
> any), and then the one of S2 (if any), and then the one of S1 (if
> any).
>
> 2. How can one modify the number and the order of the executed
> constructors (assuming one modifies S3).

Thanks for all the pointers. This is precisely what I was looking
for. I'm not yet in phase with these docs, which are obviously
extremely important.
- -- 
Merciadri Luca
See http://www.student.montefiore.ulg.ac.be/~merciadri/
- -- 

Everyone wants to go to heaven, but no one wants to die.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8 <http://mailcrypt.sourceforge.net/>

iEYEARECAAYFAk2YOMYACgkQM0LLzLt8MhwzUgCfXInbvR2jqOOkvRQnxcZp+pVq
n8EAn3sr1dsSLdBULMjRojSB+VX7rZF4
=5lDE
-----END PGP SIGNATURE-----

[toc] | [prev] | [next] | [standalone]


#497

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-04-03 08:24 -0400
Message-ID<in9ouh$k80$1@dont-email.me>
In reply to#473
On 4/2/2011 3:59 PM, Merciadri Luca wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Let us consider three classes: S3, S2 and S1. S3 is defined as a
> direct subclass of S2, which is defined as a direct subclass of S1. S1
> has a parent class, but S1's declaration does not precise its parent
> class.

     By "does not precise its parent class," I suppose you mean "does
not say `extends Whatever' in the code."  In that case, the parent
class is java.lang.Object, the ultimate parent of all classes.

> The declarations of classes S1, S2 and S3 might define 0, 1, or more
> constructors.

     One or more, yes.  Zero, no: Every class has at least one
constructor, whether you see it in the source code or not.  If the
source provides no constructors, the compiler fabricates one.

> 1. If S3 is instanciated, which constructors will be executed, and in
> which order? I thought that the one of S3 might be executed first (if
> any), and then the one of S2 (if any), and then the one of S1 (if
> any).

     S3's constructor is called first.  Before it does (almost)
anything else it calls S2's constructor, whose first (almost)
action is to call S1's constructor, whose first (almost) action
is to call Object's constructor.  Object's constructor finishes
and returns to S1's, which goes about its business and returns
to S2's, which in turn does its own work and returns to S3's,
which finishes constructing the S3 instance.

> 2. How can one modify the number and the order of the executed
> constructors (assuming one modifies S3).

     The pattern above (S3->S2->S1->Object->S1->S2->S3) always
runs as described, unless somebody throws an exception or calls
System.exit() or takes some similar drastic action that aborts
the construction.  All you can hope to control is which of several
possible S3/S2/S1 constructors is used, for classes that have more
than one.  "Assuming one modifies S3" and leaves S2/S1 alone, you
can write as many S3 constructors as you like and have them call
whatever S2 constructors are available.  Since you're not touching
S2 you can't control its choice of S1 constructors, not directly.
You may say "If I call *this* S2 constructor with *these* arguments
it will call *that* S1 constructor," but unless S2 has promised
that behavior it may change in the next version of S2.  So your
"control" is indirect.

     ... and usually undesirable, but that's another story.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#504

FromPatricia Shanahan <pats@acm.org>
Date2011-04-03 08:55 -0700
Message-ID<F5udnYlGL8ziBQXQnZ2dnUVZ_sqdnZ2d@earthlink.com>
In reply to#497
On 4/3/2011 5:24 AM, Eric Sosman wrote:
...
> The pattern above (S3->S2->S1->Object->S1->S2->S3) always
> runs as described, unless somebody throws an exception or calls
> System.exit() or takes some similar drastic action that aborts
> the construction. All you can hope to control is which of several
> possible S3/S2/S1 constructors is used, for classes that have more
> than one....

Although I agree in general with this explanation, it does ignore one
way of changing the patter of constructor calls.

A constructor can start by invoking another constructor in the same
class, instead of a superclass constructor. In that case the pattern
might be S3->S3->S2. It is useful for providing default constructor
parameters.

Patricia

[toc] | [prev] | [next] | [standalone]


#515

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-04-03 14:16 -0400
Message-ID<inadh6$a3t$1@dont-email.me>
In reply to#504
On 4/3/2011 11:55 AM, Patricia Shanahan wrote:
> On 4/3/2011 5:24 AM, Eric Sosman wrote:
> ...
>> The pattern above (S3->S2->S1->Object->S1->S2->S3) always
>> runs as described, unless somebody throws an exception or calls
>> System.exit() or takes some similar drastic action that aborts
>> the construction. All you can hope to control is which of several
>> possible S3/S2/S1 constructors is used, for classes that have more
>> than one....
>
> Although I agree in general with this explanation, it does ignore one
> way of changing the patter of constructor calls.
>
> A constructor can start by invoking another constructor in the same
> class, instead of a superclass constructor. In that case the pattern
> might be S3->S3->S2. It is useful for providing default constructor
> parameters.

     Good catch.  Thanks!

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.help


csiph-web