Sub topic 9
`var` Is a Reserved Type Name, Not a Keyword
This chapter assumes that you already know how Java declares local variables and that you have seen var used for local variable type inference.
Java treats var differently from ordinary keywords such as class, if, and return. The Java Language Specification calls var a reserved type name. This means Java reserves var for positions where a type name would appear, but it does not reserve the word in every possible position.
What “reserved type name” means
A keyword is reserved everywhere that Java parses an identifier. You cannot name a variable, method, or class class.
var has a narrower restriction. You cannot use it as the name of a type in source code, but you can use it as a variable name or method name in other contexts.
public class VarNameDemo {
public static void main(String[] args) {
int var = 10; // valid: var is a variable name
var = var + 5;
VarNameDemo demo = new VarNameDemo();
demo.var(); // valid: var is a method name
System.out.println(var);
}
void var() {
System.out.println("This method is named var.");
}
}The following declarations are rejected because var appears where Java expects a type name:
// var value; // no initializer for type inference
// var value = null; // null gives no usable type
// class var { } // var cannot be a class name
// var<String> value; // var is not a generic typeThe first two lines also fail for rules specific to local variable inference. The last two fail because var cannot name a declared type.
Context determines the meaning
The compiler examines the position of var before deciding what it means. In a local declaration with one initializer, var requests type inference. In an expression such as int var = 10, it is simply an identifier.
flowchart TD
A["Compiler reads var"] --> B{"Is var in a type position?"}
B -->|"Yes, local declaration with initializer"| C["Infer the variable's static type"]
B -->|"Yes, another type declaration"| D["Reject it as a type name"]
B -->|"No, identifier position"| E["Use var as an ordinary name"]The same spelling can therefore have different roles:
| Source context | Meaning of `var` | Result |
|---|---|---|
| `var total = 3;` | Local variable type inference | Valid |
| `int var = 3;` | Variable name | Valid |
| `void var() {}` | Method name | Valid |
| `class var {}` | Class type name | Rejected |
Source compatibility and compiler versions
Local variable type inference was added in Java 10. A compiler using an older source level does not recognize var for local inference, even though older Java code might already have used var as an ordinary variable or method name.
For example, this command asks a modern JDK to compile using Java 11 language rules:
javac --release 11 VarNameDemo.javaA project must use Java 10 or later to write declarations such as var count = 10;. The exact --release, source, or build-tool setting depends on the project, but the rule is the same: the language level must support local variable type inference.
var is not a class named java.lang.var. It does not create a dynamic value, and it does not appear in compiled bytecode as a runtime type. The compiler replaces the inferred declaration with its actual static type during compilation.
Key distinction
Think of var as a special spelling that is recognized only in particular type-related grammar positions. It is not a general-purpose keyword, and it does not mean “any type.” The initializer still determines one fixed type, and later assignments must follow that type.
Exercise
Why does int var = 4; compile while var number = 4; uses type inference?
Show answer
In int var = 4, var is an ordinary variable name because int already supplies the type. In var number = 4, var appears in the type position, so the compiler infers int from the initializer.