96. LVTI and null initializers, instance variables, and catch blocks variables

What does LVTI have in common with null initializers, instance variables, and catch blocks variables? Well, LVTI cannot be used with any of them. The following attempts will fail:

  • LVTI cannot be used with null initializers:
// result in an error of type: variable initializer is 'null'
var message = null;

// result in: cannot use 'var' on variable without initializer
var message;
  • LVTI cannot be used with instance variables (fields):
public class Player {

private var age; // error: 'var' is not allowed here
private var name; // error: 'var' is not allowed here
...
}
  • LVTI cannot be used in catch block variables:
try {
TimeUnit.NANOSECONDS.sleep(1000);
} catch (var ex) { ... }
..................Content has been hidden....................

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