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

Bad initialization of members when accessed by a super class' constructor

    XMLWordPrintable

Details

    • Bug
    • Resolution: Not an Issue
    • P4
    • None
    • 1.4.2, 6
    • tools
    • x86
    • linux, windows_xp

    Description

      FULL PRODUCT VERSION :
      java version "1.4.2_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
      Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)

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

      A DESCRIPTION OF THE PROBLEM :
      When an abstract class' constructor invokes an abstract method and the implementation of that method accesses a member of the subclass, the member isn't properly initialized and has a null value.

      Either the compiler should warn (or even throw an error) in these use-cases or the members should be initialized before the execution of the super() call.

      Please check the following steps to reproduce.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public abstract class Hello
      {

        public Hello()
        {
          abstractMethod();
        }

        public abstract void abstractMethod();

      }
      public class World extends Hello
      {
        private String xpto = "hello world";
        public World()
        {
          super();
        }

        public void abstractMethod()
        {
          System.out.println(xpto);
        }

        public static void main(String[] args)
        {
          new World();
          System.out.println("done...\n");
        }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Either the compiler should warn about the possibility of the member not being initialized or the member should be initializer prior to the execution of super().

      Another alternative is to initialize the member when it's first accessed.
      ACTUAL -
      xpto == null

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Same as steps to reproduce
      ---------- END SOURCE ----------
      ###@###.### 2005-05-24 05:54:44 GMT

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              jssunw Jitender S (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: