The order of execution for creating objects is as follows: 1. static variables initialization 2. static initializer block executition 3. constructor header (super or this) 4. instance variable initialization 5. rest of the code in the constructor Due to the fact that this(y) is being called prior to int y=1 being initialized this does not compile. If you changed it to static int y=1 then it should compile. I'm pretty sure this is correct but no doubt if i'm wrong someone will clarify.
Originally posted by Harry G: 3. constructor header (super or this) 4. instance variable initialization 5. rest of the code in the constructor
Yep, this is where the issue is. Then the constructor is first invoked, the instance variables all have their default values, as opposed to their explicit initialization values (i.e. the ones you give them at the time they're declared). It is not until *after* the super constructors have completed that the instance variables get their initial values, and then the rest of the constructor completes. cheers, Kathy
Howdy! It's supposed to be out December 9th, but I'm expecting it in mid-December. It will be on the shelves before the end of the year. I will say more when it gets closer to release. Cheers and thanks for the interest, Kathy (no cows were harmed in the making of the book, but I killed objects like there's no tomorrow) "Hand over the chocolate or the object gets it!"
1. static variables initialization 2. static initializer block execution 3. constructor header (super or this) 4. instance variable initialization 5. rest of the code in the constructor
In Code1, value = 100; is #5 (rest of the code in the constructor), and happens after instance variable initialization (#4). In Code2, value = 100; is #2 (static initializer block execution), I guess, and at that time instance variables have not yet been initialized... But, I just noticed now, at the end of my post , the initializer has no static modifier... is it a requirement for it to be static to have its execution time at, say, #2? If it is, so when will the non-static initializer be executed? Thanx, Jonathas d-_-b