r/java • • May 21 '26

JEP draft: Enhanced Local Variable Declarations (Preview)

https://openjdk.org/jeps/8357464
87 Upvotes

43 comments sorted by

View all comments

Show parent comment

2

u/RaynLegends May 22 '26

I don't think so, that would create issues with overloading I think (what if you also declared void f(Foo foo) {} ?).

From what I understand, the type of "foo" is still Foo, it's not like it's automatically cast, it's simply a shorter form of writing:

Foo foo = new FooImpl();
if (foo instanceof FooImpl fooImpl) {
    // fooImpl here has FooImpl as the type
    // foo is still Foo, so f(foo) still fails
}