r/programming • • Mar 09 '16

var keyword proposed for Java

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

395 comments sorted by

View all comments

Show parent comments

12

u/kqr Mar 10 '16

I don't see why being a functional programming language would be a requirement for making variables immutable by default. Well over 70% of the variables I use are never changed – it would be nice if I could get this guaranteed by the compiler.

0

u/immibis Mar 10 '16

You can get it guaranteed by the compiler easily, with final. The question is whether it should be the default.

And it's more general than final - in general, should the most restrictive thing be the default?

  • Should variables and fields be final by default?

  • Should methods and classes be final by default?

  • Should lambdas capture nothing by default?

  • Should methods be pure by default?

  • Should global variables be prohibited (making everything into an instance variable by default)?

(By the way, var/val/whatever makes type checking less restrictive by default, since it's easier to type var/val than the actual type, which is one less chance to confirm you didn't make a mistake)