What is var used for?
- We can declare a variable of type
var
when the actual type of the variable can be inferred based on what it is assigned to:
- Local variables with initializers &
- Indexes in enhanced for-loops and traditional for-loops
- While you can use
var
when we are assigning the result of a method call. You may not want to where the type of the return is ambiguous. While the compiler will understand that type, this practice will make the code less readable.
When can we NOT use var?
We can not use var
if the type of values being stored in the variable cannot be inferred:
- Method returns or parameters
- Constructor returns or parameters
- Variable declarations
Why use var at all?
The introduction of var
is meant to reduce the syntax when the type of a variable can be inferred.
Many other languages have something equivalent, such as: C++, C#, Scala, and Go
Java is a strongly typed language, doesn’t using var
diminish this strength? Absolutely not, since var
can only be used when the type of the variable can be inferred.
Local Variables
Below are some examples of when you can use var
for declaring and initializing local variables.
Example 1
Since age
is being assigned to 7
, we can infer that age
is of type int
. This is the same as:
Example 2
Since brady
is being assigned to a new Dog
object, we can infer that brady
is of type Dog
. This is the same as:
Example 3
Since wordlist
is being assigned to a new ArrayList
of String
objects, we can infer that wordlist
is an ArrayList
of strings. This is the same as:
For Loops
Example 1
In this example, var
is being used to declare word
. Since we know that wordlist
is an ArrayList
of String
objects, we can infer that word
is of type String
. This is the same as:
Example 2
In this example, var
is being used to declare the step variable in the for loop. This is the same as:
Not Recommended Uses of var
The following example will work, but is not recommended. While the compiler can infer the type, the someone reading the code cannot infer the type making the code less readable.
Example 1
var brady = new Dog();
var sound = brady.bark();
In this case, it is unclear what is being returned by the method bark
. It could be:
- A
String
- A
File
containing the sound of a dog barking
- A
boolean
value that represents that the dog is barking
While the compiler knows the return type, the code is less readable without knowing the return value of bark()
.
Improper Uses of var
The following are examples of when you are not allowed to use var
.
Example 1
var wordlist;
Without initialization, the compiler is unable to infer the type of wordlist
.
Example 2
var brady = null;
Since null
can be assigned to objects of any type, the compiler is unable to infer the type of brady
.
Example 3
var[] numbers = {1, 2, 3};
We need to have the type on the right side, so we could change this to:
var numbers = new int[3];
or
var numbers = new int[] {1, 2, 3};
Example 4
public var greeting(String name) {
return "hi " + name;
}
We are unable to use var
as a return type even though the return value is clear.
Example 5
public String greeting(var name) {
return "hi " + name;
}
We are unable to use var
for the type of the parameter.
Example 6
public class Dog {
String name;
public Dog(var n) {
name = n;
}
}
We are unable to use var
for constructor parameters. The type of a parameter needs to be specified and cannot be inferred.
Example 7
public class Dog {
var name;
public Dog(String n) {
name = n;
}
}
Unable to use var
for instance variables. Since name
is not instantiated when it is declared, we can not use var
.
Using var Reminders
- The use of
var
is not required.
- Using
var
can only be done when the type can be inferred. It cannot be used every where. It can be used in the following places:
- local variables with initializers
- for loops: enhanced-for and traditional
Resources
Experimenting with var in the Java Playground