86. Using LVTI in compound declarations

A compound declaration allows us to declare a group of variables of the same type without repeating the type. The type is specified a single time and the variables are demarcated by a comma:

// using explicit type
String pending = "pending", processed = "processed",
deleted = "deleted";

Replacing String with var will result in code that doesn't compile:

// Does not compile
var pending = "pending", processed = "processed", deleted = "deleted";

The solution to this problem is to transform the compound declaration into one declaration per single line:

// using var, the inferred type is String
var pending = "pending";
var processed = "processed";
var deleted = "deleted";

So, as a rule of thumb, LVTI cannot be used in compound declarations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset