Bits of Java – Episode 2: The var keyword
In this episode I would like to talk about the var
keyword, which has been introduced in Java 10, and can be used instead of the type under certain conditions.
When declaring a variable in Java, we need to specify what type of variable we want, right?
int number = 3;
String word = "A String";
Well, we could have also used var
, instead of specifying the type:
var number = 3;
var word = "A String";
This does not mean Java has become like JavaScript, for instance, where the type of var
is inferred at runtime and a variable can change its type. What is happening here is that the compiler infers the type of our variables from the initialization. If it cannot infer the type, then we would get a compiler error. And once the type has been inferred, that variable cannot change it.
The fact that the type is inferred at compiling time is basically the reason why the use of var
has some limitations.
- You cannot declare and initialize the variable in two separate statements:
//Perfectly valid declaration and initialization in the same statement
var number = 3;
//DOES NOT COMPILE
var another_number;
another_number = 3;
- You cannot use
var
for instance and class variables, but only for local variables (indeed the use ofvar
is also called local variable type inference):
public class MyClass {
var instance_variable = "I am an instance variable"; //DOES NOT COMPILE
public static var CLASS_VARIABLE = "I am a class variable"; //DOES NOT COMPILE
public void doSomething() {
var local_variable = "I am a local variable"; // OK
}
}
- You cannot declare
var
as parameter of a method or a constructor signature:
public class MyClass {
String name;
/*
* DOES NOT COMPILE: var cannot be used as parameter
* in a constructor signature
*/
public MyClass(var newName) {
name = newName;
}
/*
* DOES NOT COMPILE: var cannot be used as parameter
* in a method signature
*/
public void doSomething(var parameter) {
var local_variable = "I am a local variable"; // OK
}
/*
* DOES NOT COMPILE: var cannot be used as return type
* in a method signature
*/
public var doSomethingElse() {
var local_variable = "I am a local variable"; // OK
return local_variable;
}
}
but, pay attention:
public class MyClass {
String name;
public MyClass(String newName) {
name = newName;
}
public static void main(String[] args) {
var newName = "New Name"; //the type is inferred to String
MyClass myClass = new MyClass(newName); //OK: newName is a String
}
}
- You cannot use
var
in a multi variable declaration statement:
int number1, number2, number3; // OK
var variable = "a variable", String a_string = "a String"; //DOES NOT COMPILE
var var2 = 1.2, var3 = 4.5; //DOES NOT COMPILE
- You cannot assign
null
tovar
when declaring it, without explicitly specifying its type:
var non_allowed = null; //DOES NOT COMPILE: it does not know the type
var allowed = (String) null; //OK: it knows it is a String
- After declaring a
var
, its value can change, but its type cannot:
var number = 3;
number = 5; // OK
number = "seven"; //DOES NOT COMPILE
Another interesting fact about var
is that, being introduced only in Java 10, it is not included in the list of reserved keyword, namely it is allowed to call a variable var.
var var = 3; //OK
On the other hand, var
is a reserved type name, so it cannot be used as name for a class, a type, an interface or an enum
.
I hope you learned something new today! In the next post we will talk about one of the key features of Java: garbage collection.
By Ilenia Salvadori