Java reserved type name "var" best practices?

Viewed 450

Unfortunately, there is no item in Effective Java that discusses the reserved type name "var"(since it is introduced in java 10).

There is a saying to use interfaces as types whenever you can (Item 64: Refer to objects by their interfaces). But When using var:

var list = new ArrayList<String>(); // infers ArrayList<String>

(list is of type ArrayList<String>)

Effective Java states It is a bad practice to refer to a variable by its implementation because It makes code dependent on it. This is just one annoyance I've found when starting to use var in my code.

Question:

Are there any best practices when using the reserved type name "var"? When should I use It? When should I avoid it?

0 Answers
Related