JLS tweaks

Archie Cobbs archie at dellroad.org
Thu Mar 13 22:00:42 UTC 2014


On Thu, Mar 13, 2014 at 5:21 AM, Peter Levart <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. See also this stackoverflow
discussion<http://stackoverflow.com/questions/6881288/is-final-final-at-runtime>
.

Note also that the JVM spec specifically allows a constructor to assign
final fields before invoking super()/this(). So if the proposal we are
talking about were implemented, it should probably also allow this. So for
example this would be legal:

public class SetOfInts extends HashSet<Integer> {

    private final int max;

    public SetOfInts(int max) {
        this.max = max;   // the only thing you can do with 'this' here
        final Integer[] array = new Integer[max];
        for (int i = 0; i < max; i++)
            array[i] = i;
        super(Arrays.asList(array));
    }
}

-Archie

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


More information about the compiler-dev mailing list