Now that you understand the local variable part, it is time to go on to what type inference means. The good news is that this also means what it sounds like. When you type var, you are instructing the compiler to determine the type for you. The compiler looks at the code on the line of the declaration and uses it to infer the type. Take a look at this example:
On line 8, the compiler determines that we want an int variable. On line 9, we have no trouble assigning a different int to it. On line 10, Java has a problem. We’ve asked it to assign a String to an int variable. This is not allowed. It is equivalent to typing this: int number = “five”;
If you know a language like JavaScript, you might be expecting var to mean a variable that can take on any type at runtime. In Java, var is still a specific type defined at compile time. It does not change type at runtime.
For simplicity when discussing var, we are going to assume a variable declaration state-ment is completed in a single line. You could insert a line break between the variable name and its initialization value, as in the following example:
This example is valid and does compile, but we consider the declaration and initialization of silly to be happening on the same line.
Let’s go through some more scenarios so the exam doesn’t trick you on this topic! Do you think the following compiles?
The code does not compile. Remember that for local variable type inference, the compiler looks only at the line with the declaration. Since question and answer are not assigned values on the lines where they are defined, the compiler does not know what to make of them. For this reason, both lines 4 and 6 do not compile.
You might find that strange since both branches of the if/else do assign a value. Alas, it is not on the same line as the declaration, so it does not count for var. Contrast this behavior with what we saw a short while ago when we discussed branching and initializing a local variable in our findAnswer() method.
Now we know the initial value used to determine the type needs to be part of the same statement. Can you figure out why these two statements don’t compile?
6: | var n = null; | // DOES NOT COMPILE |
7: | } |
Line 5 wouldn’t work even if you replaced var with a real type. All the types declared on a single line must be the same type and share the same declaration. We couldn’t write int a, int v = 3; either.
Line 6 is a single line. The compiler is being asked to infer the type of null. This could be any reference type. The only choice the compiler could make is Object. However, that is almost certainly not what the author of the code intended. The designers of Java decided it would be better not to allow var for null than to have to guess at intent.
While a var cannot be initialized with a null value without a type, it can be reassigned a null value after it is declared, provided that the under-lying data type is a reference type.
Let’s try another example. Do you see why this does not compile?
public int addition(var a, var b) { // DOES NOT COMPILE return a + b;
}
In this example, a and b are method parameters. These are not local variables. Be on the lookout for var used with constructors, method parameters, or instance variables. Using var in one of these places is a good exam trick to see if you are paying attention. Remember that var is only used for local variable type inference!
There’s one last rule you should be aware of: var is not a reserved word and allowed to be used as an identifier. It is considered a reserved type name. A reserved type name means it cannot be used to define a type, such as a class, interface, or enum. Do you think this is legal?
package var;
public class Var {
public void var() {
var var = “var”;
}
public void Var() {
Var var = new Var();
}
}
Believe it or not, this code does compile. Java is case sensitive, so Var doesn’t introduce any conflicts as a class name. Naming a local variable var is legal. Please don’t write code that looks like this at your job! But understanding why it works will help get you ready for any tricky exam questions the exam creators could throw at you.