Pattern variable and field shadowing

Tagir Valeev amaembo at
Thu Nov 28 03:39:30 UTC 2019


Consider the following code:

public class A {
    private String str;

    public void f(Object obj) {
        if (obj instanceof String str) {
            System.out.println(str.toLowerCase()); // str refers to
pattern binding
        } else {
            System.out.println(str.toLowerCase()); // str refers to the field

I thought that such a code should be rejected by the compiler, as it's
confusing and could be a source of very subtle bugs. However, I
haven't found any explicit statement regarding this in the latest spec
draft [1]. Could you please clarify whether such code is acceptable
and point me to the relevant part of the spec draft. Thank you!

With best regards,
Tagir Valeev.


More information about the amber-spec-experts mailing list