Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 114
Examples with var
ОглавлениеLet's go through some more scenarios so the exam doesn't trick you on this topic! Do you think the following compiles?
3: public void doesThisCompile(boolean check) { 4: var question; 5: question = 1; 6: var answer; 7: if (check) { 8: answer = 2; 9: } else { 10: answer = 3; 11: } 12: System.out.println(answer); 13: }
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?
4: public void twoTypes() { 5: int a, var b = 3; // DOES NOT 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 underlying 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.