JLS tweaks

Archie Cobbs archie at dellroad.org
Fri Mar 14 22:09:50 UTC 2014


On Fri, Mar 14, 2014 at 8:50 AM, David M. Lloyd <david.lloyd at redhat.com>wrote:

> On 03/13/2014 05:00 PM, Archie Cobbs wrote:
>
>> On Thu, Mar 13, 2014 at 5:21 AM, Peter Levart <peter.levart at gmail.com
>> <mailto:peter.levart at gmail.com>> wrote:
>>
>>     On 03/13/2014 11:08 AM, Peter Levart wrote:
>>
>>         I don't see this as any more complex or unsafe as the rules for
>>         final fields. But I don't know if it is compatible with JVM rules.
>>
>>
>>     Does JVM have any rules for final instance fields regarding their
>>     definitive assignment at all non-exceptional exit paths of the
>>     constructor?
>>
>>
>> My understanding is that the JVM doesn't care how many times you assign
>> a final field in a constructor, it only cares that you don't try to
>> assign it from within a non-constructor method.
>>
> [...]
>
>
>  Note also that the JVM spec specifically allows a constructor to assign
>> final fields before invoking super()/this().
>>
>
> I think you're mistaken on both of these points... can you quote a section
> of the JLS that says this?
>

You said "JLS" ... not sure if that was just a typo not... I'm talking here
about the JVM spec, not the JLS.

I'm basing this on sect
4.10.2.4<http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.10.2.4>which
says in part:

The instance initialization method
(§2.9<http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.9>)
> for class myClass sees the new uninitialized object as its this argument
> in local variable 0. Before that method invokes another instance
> initialization method of myClass or its direct superclass on this, the
> only operation the method can perform on this is assigning fields
> declared within myClass.
>

That covers the second point (constructors can assign final fields prior to
super()/this()).

On the first point (final fields can be assigned more than once in a
constructor) from what I can tell there is nothing in the JVM spec that
explicitly prevents it.

-Archie

-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20140314/fe76b0fe/attachment.html>


More information about the compiler-dev mailing list