Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8310133

Effectivelly final condition not enforced in guards for binding variables from the same case

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 22
    • 21
    • tools
    • b03

        Consider this snippet of code:
        ---
        public class Test {

            public static void main(String[] args) {
                Object o = null;
                switch (o) {
                    case String s when s.isEmpty() -> {
                        s = null;
                    }
                    default -> {}
                }
            }
        }
        ---

        javac accepts this code, even though 's' is not effectively final and is used inside the guard. But, variables declared outside of the guard must be (effectively) final.

              jlahoda Jan Lahoda
              jlahoda Jan Lahoda
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: