Compiler confused by parenthesized "this" in final fields assignments

Victor Williams Stafusa da Silva victorwssilva at gmail.com
Fri Jun 8 17:39:38 UTC 2018


What about this?

public class AssignBug {
    private final int y;
    private final AssignBug x = this;
    private final int z = x.y;
    private final int w = getY();

    {
        y = 42;
    }

    public static void main(String[] args) {
        AssignBug h = new AssignBug();
        System.out.println(h.y + "|" + h.z + "|" + h.w);
    }

    public int getY() {
        return y;
    }
}


By assign "this" to some other variable and acessing the unitilized field
of that variable, it becomes easy to defeat the final variable
initialization check. It can also be defeated by acessing it through a
method.

The JLS (chapter 16) says:

Each local variable (§14.4
<https://docs.oracle.com/javase/specs/jls/se10/html/jls-14.html#jls-14.4>)
and every blank final field (§4.12.4
<https://docs.oracle.com/javase/specs/jls/se10/html/jls-4.html#jls-4.12.4>,
§8.3.1.2
<https://docs.oracle.com/javase/specs/jls/se10/html/jls-8.html#jls-8.3.1.2>)
must have a *definitely assigned* value when any access of its value occurs.

An access to its value consists of the simple name of the variable (or, for
a field, the simple name of the field qualified by this) occurring anywhere
in an expression except as the left-hand operand of the simple assignment
operator = (§15.26.1
<https://docs.oracle.com/javase/specs/jls/se10/html/jls-15.html#jls-15.26.1>
).

For every access of a local variable or blank final field x, x must be
definitely assigned before the access, or a compile-time error occurs.


Saying "must be definitely assigned before the access, or a compile-time
error occurs" is a very bold statement  that is simply unenforceable and
broken. The chapter 16 details many of the rules when a variable should be
considered definitely assigned or definitely unassigned, but it overlooks
those two cases.

Victor Williams Stafusa da Silva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180608/ee046b1c/attachment.html>


More information about the compiler-dev mailing list