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


Groups > comp.lang.java.help > #478

Re: Constructors order of invocation

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.help
Subject Re: Constructors order of invocation
Date 2011-04-02 17:20 -0400
Organization albasani.net
Message-ID <in83vc$ekt$1@news.albasani.net> (permalink)
References <878vvsqvat.fsf@merciadriluca-station.MERCIADRILUCA>

Show all headers | View raw


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

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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