This is expected behavior. The following is an excerpt from "The Java
Language Specification", sectoion 12.5 (Creation of New Class
Instances):
----cut here-----
1.Assign the arguments for the constructor to newly created parameter
variables for this constructor invocation.
2.If this constructor begins with an explicit constructor invocation
of another constructor in the same class (using this), then evaluate the
arguments and process that constructor
invocation recursively using these same five steps. If that
constructor invocation completes abruptly, then this procedure completes
abruptly for the same reason; otherwise,
continue with step 5.
3.This constructor does not begin with an explicit constructor
invocation of another constructor in the same class (using this). If
this constructor is for a class other than Object,
then this constructor will begin with a explicit or implicit
invocation of a superclass constructor (using super). Evaluate the
arguments and process that superclass constructor
invocation recursively using these same five steps. If that
constructor invocation completes abruptly, then this procedure completes
abruptly for the same reason. Otherwise,
continue with step 4.
4.Execute the instance variable initializers for this class,
assigning their values to the corresponding instance variables, in the
left-to-right order in which they appear textually in the
source code for the class. If execution of any of these
initializers results in an exception, then no further initializers are
processed and this procedure completes abruptly with that
same exception. Otherwise, continue with step 5. (In some early
Java implementations, the compiler incorrectly omitted the code to
initialize a field if the field initializer expression
was a constant expression whose value was equal to the default
initialization value for its type.)
5.Execute the rest of the body of this constructor. If that execution
completes abruptly, then this procedure completes abruptly for the same
reason. Otherwise, this procedure
completes normally.
----cut here-----
Note that item 4 (executing instance vairable initializers) follows 2
and 3 (superclass constructor calls). I did not read Bruce Eckel's book
so I cannot say whether he is wrong or just misinterpreted.
Hope this helps
All the Best
Pavel
Richard Jones wrote:
>
> Can someone tell me why this program, when run under
> Linux, prints d = 0 (ie. the variable d isn't being
> initialized as expected)? According to Bruce Eckel's
> book, which is the only reference I have available to
> me, d should be initialized to 1, so I suspect this
> may be a JVM bug ...
>
> Rich.
>
> ---------------------------------------------------------
> /* SuperClass.java */
>
> public abstract class SuperClass
> {
> protected SuperClass ()
> {
> method ();
> }
>
> protected abstract void method ();
> }
> ---------------------------------------------------------
>
> ---------------------------------------------------------
> /* DerivedClass.java */
>
> public class DerivedClass extends SuperClass
> {
> public int d = 1;
>
> public DerivedClass ()
> {
> super ();
> }
>
> protected void method ()
> {
> System.out.println ("d = " + d);
> }
>
> public static void main (String[] args)
> {
> DerivedClass derivedClass = new DerivedClass ();
> }
> }
> ---------------------------------------------------------
>
> $ java -version
> java version "1.1.6"
> $ java DerivedClass
> d = 0
>
> --
> - Richard Jones. Linux contractor London and SE areas. -
> - Very boring homepage at: http://www.annexia.demon.co.uk/ -
> - You are currently the 1,991,243,100th visitor to this signature. -
> - Original message content Copyright (C) 1998 Richard Jones. -