Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

All these options will be set automatically, when importing inspections settings from the repository.

2 Using var keyword

The local variable type inference is the powerful feature of Java language. Nevertheless, one of the main goals of the code style guide is to ensure readability for developers, not for the compiler.

Based on this statement there are limited uses cases where var keyword is allowed. The var keyword is allowed only in cases where a right part of an assignment expression contains an explicit type of variable, that is for assignments of literals and the result of constructor invocation.

Allowed only for assignments of literals and result of constructor invocation:

Code Block
languagejava
var i = 42;
var l = 42L;
var s = "Hello";
var list = new ArrayList<Integer>();


Disallowed for all other cases:

Code Block
languagejava
var i = Integer.parseInt("42");
var s = "Hello".substring(0, 3);
var list = Arrays.asList(1, 2, 3);        
var map = getMapping(); // some method invocation
var val = map.get(42);