Why does this() and super() have to be the first statement in a constructor?
Ulf Zibis
Ulf.Zibis at gmx.de
Sun Oct 9 06:00:32 PDT 2011
Am 09.10.2011 04:42, schrieb Joe Darcy:
> Ulf Zibis wrote:
>> Anyway,
>> int c = a + b;
>> doesn't affect any object field initialization.
>> It's just a local variable, which gets lost after the instance is instantiated.
>>
>> As workaround, you can always use a static helper method which does the same:
>> super(staticHelper(a, b));
>>
>> So I agree, it would reasonable, to allow some code in advance of this() or super().
>>
>> + 1 for your proposal, Vimil Saju.
>
> This is not a proposal, it is a question!
Yes, you are correct, I had implied it behind, as "question" seemed too innocent to me.
Please apologize my ease, not having referred a dictionary. English is not my mother tongue.
Now I did, and assume, "challenge" would have been the best wording.
>
> The answer to the question has already been given on the list:
But it is improper, because Vimil's example doesn't affect the guarantee , the superclass to be
initialized first before the subclass.
> ... in an attempt to reduce logic errors in initialization.
This is fine, but on one hand, the standard is too strict, and in prominent corner cases, e.g. inner
classes, the guarantee is violated, but could be too "repaired":
Bug 7091048 <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7091048> - Compiler should detect
race conditions on final fields
>
> This restriction is annoying at time, but no one has put forward a detailed proposal for how the
> requirement could be sensibility loosened in a way that can be standardized and tested.
I allege, the test is already there: If a member variable or method is referred as parameter of
super() or this(), the compiler reports an error. So I imply, there already exists a suitable
standard to fulfil the asked requirement.
-Ulf
>
> -Joe
>
>> -Ulf
>>
>>
>>
>> Am 08.10.2011 01:39, schrieb Daniel Yokomizo:
>>> On Oct 7, 2011 1:58 PM, "Paul Benedict"<pbenedict at apache.org> wrote:
>>>> It's a compiler error because the superclass is guaranteed to be
>>>> initialized first before the subclass.
>>> Not guaranteed by the JVM, as in anonymous inner classes.
>>>
>>>> On Fri, Oct 7, 2011 at 11:51 AM, Vimil Saju<vimilsaju at yahoo.com> wrote:
>>>>> If you have subclass then java requires that this() or super has to be
>>> first statement in the constructor of the subclass.
>>>>> Here is an example
>>>>>
>>>>> publicclassMyClass{
>>>>> publicMyClass(intx){}
>>>>> }
>>>>>
>>>>> publicclassMySubClassextendsMyClass{
>>>>> publicMySubClass(inta,intb){
>>>>> intc =a +b;
>>>>> super(c); // COMPILE ERROR
>>>>> }
>>>>> }The above compilation error can be resolved by rewriting the code in
>>> the constructor as follows
>>>>> publicclassMySubClassextendsMyClass{
>>>>> publicMySubClass(inta,intb){
>>>>> super(a + b);
>>>>> }
>>>>> }Can't the Java compiler detect that in the previous code there was no
>>> access to the instance fields or methods and therefore allow the code to
>>> compile without any error.
>>
>
>
More information about the coin-dev
mailing list