21

In the last issue of Heinz Kabutz's newsletter, #255 Java 10: Inferred Local Variables, it is shown that var is not a reserved word in Java 10, because you can also use var as an identifier:

public class Java10 {
    var var = 42; // <-- this works
}

However, you cannot use i.e. assert as an identifier, as in var assert = 2, because assert is a reserved word.

As it's told in the linked newsletter, the fact that var is not a reserved word is good news, because this allows code from previous versions of Java that uses var as an identifier to compile without problems in Java 10.

So, what's var then? It's neither an explicit type nor a reserved word of the language, so it's allowed to be an identifier, however it does have a special meaning when used to declare a local variable in Java 10. What exactly do we call it in the context of a local variable declaration?

Additionally, apart from supporting backwards compatibility (by allowing older code that contains var as an identifier to compile), are there other advantages to var not being a reserved word?

16
  • 2
    @FedericoPeraltaSchaffner The byte code with var var = 42 or Integer var = 42 is exactly the same; it's just for the compiler here, so it's a type name. Notice that this is not very different than Integer Integer = 42 (upvote to re-open, really wanted to make this one an answer too)
    – Eugene
    Mar 4, 2018 at 11:03
  • 2
    I've edited the canonical answer. Mar 5, 2018 at 0:47
  • 2
    @bhspencer Correction; it was updated from the disjunction of the two (reflecting the options out the outset of the project) to "reserved type identifier" (reflecting the final outcome.) JEPs are living documents; it is common for the design to evolve between start and end, and ideally we update the JEP to reflect reality as we go. Mar 5, 2018 at 0:50
  • 2
    @bhspencer Because restricted type identifiers were a simpler and cleaner way to specify and implement the desired result. We could have used a restricted keyword, but it would have been more intrusive in the grammar and specification, for no incremental benefit. Mar 5, 2018 at 1:03
  • 2
    The specification drafts for this features has been available publicly for a year; final version (prior to integration into the master spec) is here: bugs.openjdk.java.net/secure/attachment/72914/… (linked from bugs.openjdk.java.net/browse/JDK-8151553). Mar 5, 2018 at 2:05

2 Answers 2

22

According to JEP-286: Local-Variable Type Inference, var is

not a keyword; instead it is a reserved type name.

(Earlier versions of the JEP left room for implementing either as a reserved type name or as a context-sensitive keyword; the former path was ultimately chosen.)

Because it's not a "reserved keyword", it is possible to still use it in variable names (and package names), but not in class or interface names.

I would think the biggest reason for not making var a reserved keyword is backwards compatibility with old source code.

12
  • 3
    @bhspencer A keyword is explicitly defined in 3.9. A context-sensitive keyword is something else. There's no "broken" here. Mar 3, 2018 at 21:35
  • 5
    JLS:3.9 provides a really clear definition of keyword: you cannot use one as an identifier. So in this sense, var really isn't a keyword. Mar 3, 2018 at 21:35
  • 3
    By the way. Context sensitive keywords are not new to Java. They were already in Java 9. For example, you can declare module module. Mar 4, 2018 at 2:52
  • 7
    A context sensitive keyword is a character sequence that is recognized as having syntactic meaning in specific productions, but is otherwise treated as an ordinary identifier. (This is typically managed in the parsing stage of compilation.) A reserved type identifier is an an identifier that may not be used as the name of a type (this is handled in the attribution stage of compilation.) Mar 5, 2018 at 0:54
  • 2
    @BrianGoetz bhspencer has opened a new question What is the conceptual difference between a “restricted keyword” and “reserved type identifier” in Java 10? This may be part of a good answer to that question. Mar 5, 2018 at 2:21
7

var is a reserved type name var is not a keyword, It’s a reserved type name.

We can create a variable named “var”.

you can read here for more details.

var var = 5; // syntactically correct
// var is the name of the variable
“var” as a method name is allowed.

public static void var() { // syntactically correct 
}
“var” as a package name is allowed.

package var; // syntactically correct
“var” cannot be used as the name of a class or interface.
class var{ } // Compile Error
LocalTypeInference.java:45: error: 'var' not allowed here
class var{
      ^
  as of release 10, 'var' is a restricted local variable type and cannot be used for type declarations
1 error

interface var{ } // Compile Error

var author = null; // Null cannot be inferred to a type 
LocalTypeInference.java:47: error: cannot infer type for local variable author
                var author = null;
                    ^
  (variable initializer is 'null')
1 error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.