Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #497
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Constructors order of invocation |
| Date | 2011-04-03 08:24 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <in9ouh$k80$1@dont-email.me> (permalink) |
| References | <878vvsqvat.fsf@merciadriluca-station.MERCIADRILUCA> |
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
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web