Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6596304

Object state which was set during construction is lost

XMLWordPrintable

    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0_02"
      Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      Have two classes where class Child extends class Parent, and both defining one instance field of some reference type (e.g. java.lang.String).

      Have a method for initializing those instance fields so that class Parent initializes its own field and class Child its own field. Have this initialization method invoked from the default non-args constructor of class Parent.

      Both fields get initialized when an instance of Child is _being_ constructed. However, when the constructor completes, the state of the field in class Parent is preserved, but the state of the field in class Child is lost.

      It appears to be a JDK 5.0/6.0 compiler problem, because when the sample is compiled using JDK 1.0.2 compiler (sic!) and run, there is no such problem. Also, when removing the invocation of the initialization method from the constructor and invoking it explicitly, there is no such problem

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile the sample code provided, execute class Main.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      "java Main" should print:

      Child#init
              parentField is parentField
              childField is childField
      Main#main
              parentField is parentField
              childField is childField
      ACTUAL -
      "java Main" prints:

      Child#init
              parentField is parentField
              childField is childField
      Main#main
              parentField is parentField
              childField is null

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Parent {

      protected String parentField = null;

      public Parent(){
      init();
      }

      protected void init(){
      this.parentField = "parentField";
      }
      }

      public class Child extends Parent {

      protected String childField = null;

      @Override
      protected void init(){
      super.init();

      this.childField = "childField";

      debug("Child#init");
      }

      public void debug(String message){
      System.out.println(message);
      System.out.println("\t" + "parentField is " +super.parentField);
      System.out.println("\t" + "childField is " +this.childField);
      }
      }

      public class Main {

      public static void main(String[] args){
      Child child = new Child();
      child.debug("Main#main");
      }
      }
      ---------- END SOURCE ----------

            abuckley Alex Buckley
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: