// Class InvalidLVTI illustrates invalid use of the restricted type name var.
public class InvalidLVTI {
var javaVendor = “Oracle”; // (19) Not allowed in instance variable declaration.
static var javaVersion = 11; // (20) Not allowed in static variable declaration.
public static void main(var args) { // (21) Not allowed for method parameters.
var name; // (22) Not allowed without initialization expression.
var objRef = null; // (23) Literal null not allowed.
var x = 10.0, y = 20.0, z = 40; // (24) Not allowed in compound declaration.
var vowelsOnly = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’ }; // (25) Array initializer not
// allowed.
var attendance = new int[]; // (26) Non-empty dimension required.
var array3Dim = new String[][2][]; // (27) Cannot specify an empty dimension
// before a non-empty dimension.
var letters[] = new char[]{‘a’, ‘e’, ‘i’, ‘o’, ‘u’ }; // (28) var not allowed
// as element type.
var prompt = prompt + 1; // (29) Self-reference not allowed in
// initialization expression.
}
public static var getPlatformName() { // (30) Not allowed as return type.
return “JDK”;
}
}
The following examples of invalid uses of the restricted type name var are shown in the class InvalidLVTI in Example 3.17:
- Not allowed in field variable declarations
The var restricted type name is not allowed in field variable declarations, as shown at (19) and (20).
- Not allowed in declaring formal parameters
Formal parameters in methods and constructors cannot be declared with var, as shown at (21) for the parameter args in the main() method.
- Initialization expression is mandatory
The var restricted type name is not allowed in a local variable declaration if an initialization expression is not specified, as shown at (22).
- Initialization expression cannot be the null literal value
Since the literal null can be assigned to any reference type, a specific type for objRef at (23) cannot be determined. At (5), the cast (String) specifies the type of the initialization expression.
- Cannot use var in compound declarations
The reserved type name var cannot be used in a compound declaration—that is, a declaration that declares several variables, as shown at (24).
- Cannot use var when an array initializer is specified
As shown at (25), an array initializer cannot be used in a var declaration. However, an array initialization expression is allowed, as at (11a).
- Array creation expression must specify the size
As in the case when an explicit type is specified for an array variable, the array creation expressions in the declaration must also specify the array size when using var; otherwise, the compiler will issue an error, as at (26) and (27). Valid array creation expressions specifying correct size are shown at (11b), (11c), and (11d).
- Cannot use var as an array element type
The square brackets ([]) on the left-hand side at (28) are not allowed, as they indicate that the local variable is an array. Array type and size are solely determined from the initialization expression, as at (11a), (11b), (11c), and (11d).
- Cannot have a self-reference in an initialization expression
As in the case when an explicit type is specified for the local variable, the initialization expression cannot refer to the local variable being declared, as at (29), where the variable is not initialized before use.
- Cannot use var as the return type of a method
The method declaration at (30) cannot specify the return type using var.
- A type cannot be a named var
As var is a reserved type name, it is not a valid name for a reference type; that is, a class, an interface, or an enum cannot be named var. In other contexts, it can be used as an identifier, but this is not recommended.
public class var {} // var is not permitted as a class name. Compile-time error!
The reserved type name var should be used judiciously as the code can become difficult to understand. When reading the local declaration below, the initialization expression does not divulge any information about the type, and the names are not too helpful:
var x = gizmo.get();
Unless it is intuitively obvious, a human reader will have to resort to the API documentation in order to infer the type. Using intuitive names becomes even more important when using the reserved type name var.
We will revisit the restricted type name var when discussing exception handling with try-with-resources (§7.7, p. 407), using generics in local variable declarations (§11.2, p. 571), and specifying inferred-type lambda parameters (§13.2, p. 680).