Re: Init'ing fields
From: Tom Dyess (tdyess_at_dysr.com)
Date: 01/05/05
- Next message: P. Barthelemy: "Re: Unsatisfied Link Error with JNI example"
- Previous message: allelopath: "Unsatisfied Link Error with JNI example"
- In reply to: John C. Bollinger: "Re: Init'ing fields"
- Next in thread: Tony Morris: "Re: Init'ing fields"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: P. Barthelemy: "Re: Unsatisfied Link Error with JNI example"
- Previous message: allelopath: "Unsatisfied Link Error with JNI example"
- In reply to: John C. Bollinger: "Re: Init'ing fields"
- Next in thread: Tony Morris: "Re: Init'ing fields"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|