r/java • • Mar 09 '16

var keyword proposed for Java

http://openjdk.java.net/jeps/286
240 Upvotes

312 comments sorted by

View all comments

-3

u/slartybartfast_ Mar 09 '16

My argument against it is code like this:

var c = Class.forName("com.foo.Bar")

where you don't know the type of 'c' unless you know the type the method returns (which is not obvious by looking at it)

9

u/ElvishJerricco Mar 09 '16

That expression would actually return Class<?>. So you would have to cast it to Class<Bar> with or without the var keyword. Doing this solves your problem.

var c = (Class<Bar>) Class.forName("com.foo.Bar");

-6

u/slartybartfast_ Mar 09 '16

Right, so I have I saved keystrokes here? I think not!

10

u/ElvishJerricco Mar 09 '16

Hm? The point is that with or without var, you have to perform the cast. Without var, it still looks like this.

Class<Bar> c = (Class<Bar>) Class.forName("com.foo.Bar");

With var, you get to remove a completely unnecessary type declaration.

→ More replies (8)