Re: Init'ing fields

From: Tom Dyess (tdyess_at_dysr.com)
Date: 01/05/05


Date: Tue, 4 Jan 2005 18:16:21 -0500

Whoops. Good eye.

"John C. Bollinger" <jobollin@indiana.edu> wrote in message
news:cre9ga$nli$1@hood.uits.indiana.edu...
> Tom Dyess wrote:
>
>> Yes and no, the latter is a shortcut to the former. It DOES run the
>> constructor code before the inline declaration/creation objects
>> definitions though.
>
> Careful. Variable initializers and instance initializers are run after
> the *superclass'* constructor but before the (rest of) the constructor
> body for the class in which they appear. This is as specified in JLS(2e)
> 8.8.6. It is easy to write a test class that demonstrates this behavior:
>
>
> public class InitOrder {
> int initWithInitializer = 42;
> int initInConstructor;
>
> public InitOrder() {
> /*
> * Implicit invocation of Object() occurs here
> *
> * The instance variable initializer then runs before the
> * (rest of) the constructor body.
> */
>
> System.out.println("At constructor start:");
> System.out.println(" initWithInitializer = "
> + initWithInitializer);
> System.out.println(" initInConstructor = "
> + initInConstructor);
>
> /*
> * The last println() above shows initInConstructor with its
> * default value
> */
>
> initInConstructor = 17;
>
> System.out.println("At constructor end:");
> System.out.println(" initWithInitializer = "
> + initWithInitializer);
> System.out.println(" initInConstructor = "
> + initInConstructor);
> }
>
> public static void main(String[] args) {
> InitOrder instance = new InitOrder();
>
> System.out.println("After constructor:");
> System.out.println(" initWithInitializer = "
> + instance.initWithInitializer);
> System.out.println(" initInConstructor = "
> + instance.initInConstructor);
> }
> }
>
>
> ====
>
> D:\temp\testdir>java -cp . InitOrder
> At constructor start:
> initWithInitializer = 42
> initInConstructor = 0
> At constructor end:
> initWithInitializer = 42
> initInConstructor = 17
> After constructor:
> initWithInitializer = 42
> initInConstructor = 17
>
> ====
>
>
> John Bollinger
> jobollin@indiana.edu



Relevant Pages

  • Re: Initing fields
    ... > constructor code before the inline declaration/creation objects definitions ... Variable initializers and instance initializers are run after ... the *superclass'* constructor but before the the constructor ... public class InitOrder { ...
    (comp.lang.java.programmer)
  • Re: Equivalent code to the bool() built-in function
    ... candide writes: ... it appears that it's _semantically_ a shortcut for ... So of course the ‘bool’ constructor will return them. ... The Boolean type is particularly ...
    (comp.lang.python)
  • Re: Confused about line of code in book
    ... that's the delimiter for the call to the base class' constructor. ... > shortcut to avoid having to call the base class' constructor from the ... protected NameValueList(SerializationInfo info, StreamingContext context) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Confused about line of code in book
    ... >> shortcut to avoid having to call the base class' constructor from the ... how would you call the base constructor from the ... / construct is there to avoid having the developer to call the base class' ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Using reflection
    ... > Joona I Palaste wrote: ... why do we have a "shortcut" ... with calling newInstanceon Class instead of Constructor for the ...
    (comp.lang.java.programmer)