Variable not in scope after instanceof

John Hendrikx hjohn at xs4all.nl
Thu Apr 20 07:20:34 UTC 2023


I came across a bit of a curious way to check if something is not an 
instance of something, and I noticed it doesn't play along with the new 
instanceof construct that allows you to assign it to a local variable 
immediately:

		    public static void main(String[] args) {
       Number n = 1.0;

       if (n instanceof Double d == false) {
         System.out.println("It was not a double");

         return;
       }

       System.out.println("It was a double: " + d);   // compile error
     }

In the above snippet, the variable "d", despite being accepted, is not 
in scope anywhere.

Changing the check to " !(n instanceof Double d)" works as expected.

Is this something that was missed, or am I misunderstanding why this 
can't work?

--John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230420/8681d6c2/attachment.htm>


More information about the amber-dev mailing list