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

Superclass Constructors Must be First

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.5
    • specification
    • x86
    • windows_nt



      Name: dgC58589 Date: 01/28/98


      Java is unnecessarily strict in its requirement that a
      superclass constructor must be called before any other
      operations can occur in a subclass constructor. For
      example, the following code causes a compiler error
      (actually two compiler errors) because the if() statement
      appears before the call to the superclass constructor:

         import java.awt.Dialog;
         import java.awt.Frame;

         public class MyDialog extends Dialog
         {
            private static final Frame dummyFrame = new Frame();

            // This constructor will not compile!
            public MyDialog(Frame f, String title, boolean isModal)
            {
               if(f == null)
                  f = dummyFrame;

               // The call to the superclass constructor must occur
               // as the very first operation in the subclass constructor.
               super(f, title, isModal);
            }

            public static void main(String[] args)
            {
               MyDialog md = new MyDialog(null, "ABC", true);
            }
         }

      In my opinion, it is extremely common to perform some form
      of initialization or checking prior to invoking a superclass
      constructor—the initialization may in fact determine which
      superclass constructor is appropriate to invoke.

      I would propose the following change to Java's requirement
      that the superclass constructor call be the very first
      operation in a subclass constructor:

            A subclass constructor must call a version of its
            superclass constructor before the subclass
            constructor attempts any of the following:

                  1) Making a reference to a field (method or
                  variable) defined in the superclass. Note
                  that such a reference includes any
                  mention of the this object.
                  
                  2) Returning control from the subclass
                  constructor.

            If the subclass constructor does not explicitly
            call a superclass constructor, the superclass
            default constructor is implicitly called before
            any statements in the subclass constructor are
            performed.
      (Review ID: 23960)
      ======================================================================

            Unassigned Unassigned
            dgrahamcsunw David Graham-cumming (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: