Quick revision
The short answer
int goto; does not compile, because goto is a reserved keyword that Java never uses. int var; and String record; both compile, because var and record are contextual keywords that are special only in certain positions, and as variable names they are allowed. int yield; also compiles as a local variable in current versions, although yield cannot be used as an unqualified method call name (check your version). int _; does not compile: underscore has been a keyword since Java 9, and since Java 22 it declares an unnamed variable, which needs an initializer, so int _ = 1; is valid but declares nothing usable. Finally, true, false and null are literals rather than keywords, yet they still cannot be identifiers.
What to remember
In this answer, we saw that Java has three kinds of special words. Reserved keywords such as goto and const can never be identifiers, so int goto; fails. Contextual keywords such as var, record and yield are special only in certain positions, so int var; and String record; compile, and int yield; compiles as a local variable, with restrictions on yield as a method name. The underscore became a keyword in Java 9 and an unnamed variable in Java 22, so int _; fails while int _ = 1; is an unnamed declaration. And true, false and null are literals that still cannot be identifiers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
int goto; does not compile, because goto is a reserved keyword that Java never uses. int var; and String record; both compile, because var and record are contextual keywords that are special only in certain positions, and as variable names they are allowed. int yield; also compiles as a local variable in current versions, although yield cannot be used as an unqualified method call name (check your version). int _; does not compile: underscore has been a keyword since Java 9, and since Java 22 it declares an unnamed variable, which needs an initializer, so int _ = 1; is valid but declares nothing usable. Finally, true, false and null are literals rather than keywords, yet they still cannot be identifiers.
Why this question is asked
Most of us never think about which words are reserved until a library upgrade or a Java upgrade breaks a build, or a code generator produces a field called record. Interviewers ask this question because the answer shows how the language has grown: some words were reserved from the start, others were added later in a way designed not to break old code. In this answer, we will recap what an identifier is, go through the three kinds of special words, reserved keywords, contextual keywords and literals, check each declaration in the question, and finish with the story of the underscore.
1A Quick Recap: Identifiers and Special Words
An identifier is the name we give to a variable, method, class or package. It must start with a letter, a dollar sign or an underscore, followed by letters, digits, dollar signs or underscores. But some sequences that follow those rules are taken by the language itself, and there are three kinds of them.
Reserved keywords, such as class, int, if and goto, can never be identifiers anywhere. Contextual keywords, such as var, record, yield, sealed, permits and non-sealed, are special only in particular places in the grammar and can be ordinary names elsewhere. Literals, true, false and null, are values rather than keywords, but the specification still forbids them as identifiers. The table sums up the five declarations in the question.
| Declaration | Compiles? | Why |
|---|---|---|
| int goto; | no | goto is a reserved keyword, never used by the language |
| int var; | yes | var is contextual: special only as a type name |
| String record; | yes | record is contextual: special only when declaring a type |
| int yield; | yes, check your version | yield is contextual: special as a statement and a method name |
| int _; | no | keyword since Java 9; since 22 an unnamed variable needing an initializer |
2Reserved Keywords: goto and const
Java reserves goto and const even though no Java statement uses them. The designers reserved them on purpose so that C and C++ programmers would get a clear compile error instead of code that meant something unexpected, and so that the words would be free if the language ever needed them. Being reserved means they can never be identifiers, in any position.
Notice that keywords are case-sensitive, so goTo or Goto are ordinary names. That is legal, but clearly a bad idea in real code.
3Contextual Keywords: var, record and yield
When Java added new features after 2014, it faced a problem: words like var and record were already used as variable names in millions of lines of code. Making them full keywords would break all of that code. So the language made them contextual. var is special only where a type is expected, so it cannot be the name of a class, but it can be a variable or method name. record is special only when declaring a type, sealed, non-sealed and permits only in class declarations, and yield only at the start of a statement inside a switch expression.
yield has one extra restriction. Inside a switch expression, yield value; returns the value of an arm, so the compiler would not know whether yield(1); is that statement or a call to a method called yield. To avoid the ambiguity, newer versions reject an unqualified call to a method named yield; we must write this.yield(1) or ClassName.yield(1). As a local variable name it still compiles, but the exact rules have been tightened over several releases, so check your version before relying on it.
4The Story of the Underscore
The underscore is the one case where Java did reclaim a word from ordinary code. In Java 8, a variable named with a single underscore compiled with a warning. In Java 9 it became a keyword, and int _ = 1; became an error. Then, in Java 22, unnamed variables became final: a single underscore now declares a variable we promise never to use, such as an exception we ignore or a lambda parameter we do not need.
So int _; fails in every current version, though for different reasons: in Java 8 it still compiled with a warning, from 9 to 21 it is a keyword that cannot be an identifier, and from 22 it is an unnamed declaration, which as far as we know must have an initializer. int _ = 1; compiles in Java 22 and later but declares nothing we can read. The exact error messages differ between releases, so check your version.
5Literals: true, false and null
true and false are boolean literals and null is the null literal. Technically they are not keywords, which is why the specification lists them separately, but the effect for us is the same: they can never be identifiers. A literal is a value written directly in the code, just like 42 or "text", so letting it be a name would make expressions ambiguous.
6Follow-up questions
Why did Java make var contextual instead of a real keyword?
To keep source compatibility. Existing code with variables, fields or methods named var kept compiling unchanged; only classes named var broke, and those were very rare because class names start with a capital letter by convention.
Can a method be called record or var?
Yes. Both are allowed as method names, and record is commonly used as a field or method name in older code. They are only restricted as type names, which is where the new grammar needs them.
What are all the contextual keywords today?
The list in the specification includes exports, module, non-sealed, open, opens, permits, provides, record, requires, sealed, to, transitive, uses, var, when, with and yield. Most belong to module declarations, where they are only special inside module-info.java.
7Conclusion
In this answer, we saw that Java has three kinds of special words. Reserved keywords such as goto and const can never be identifiers, so int goto; fails. Contextual keywords such as var, record and yield are special only in certain positions, so int var; and String record; compile, and int yield; compiles as a local variable, with restrictions on yield as a method name. The underscore became a keyword in Java 9 and an unnamed variable in Java 22, so int _; fails while int _ = 1; is an unnamed declaration. And true, false and null are literals that still cannot be identifiers.
Open with
Of the five, int goto does not compile, because goto is a reserved keyword. Java reserves goto and const without using them, so they can never be names. int var and String record both compile, because those are contextual keywords.
Then, if they want more
When Java added them it could not make them full keywords without breaking existing code, so var is only special where a type is expected, which means I cannot name a class var but I can name a variable var, and record is only special when declaring a type. int yield also compiles as a local variable in current versions.
The restriction there is that an unqualified call to a method named yield is rejected, because it would clash with the yield statement in switch expressions, but I would check the exact rules for the version in use. int _ does not compile.
The underscore became a keyword in Java 9, and since Java 22 it declares an unnamed variable, so int _ = 1 runs the initializer but gives me nothing I can read, and an unnamed local needs an initializer. Lastly, true, false and null are literals rather than keywords, but they are still forbidden as identifiers.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 22, Java 9, Java 8, Java 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Which of these compile as identifiers in current Java and why: int goto; int var; String...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/a514c78585/