-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.1.5
-
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)
======================================================================
- duplicates
-
JDK-4093999 Relax constraint on placement of super() call in constructors
-
- Closed
-