Java Keywords
In cricket, some words have a fixed meaning: "over", "wicket", "no-ball". You can't name your team "No-ball" and expect the umpire to understand. Java has a list of words like that too: keywords. Each one has a fixed meaning to the compiler, so you can't use it as the name of a variable, method or class.
What a Keyword Is
A keyword is a word that is part of Java's grammar. When the compiler sees class, if or return, it knows exactly what you mean. That's why the same word can't also be your variable's name:
KwClass.java:3: error: not a statement
int class = 5;
^The compiler reads class as the start of a class declaration, gets confused, and reports an error that doesn't even mention keywords. When you see a strange error on a line with an ordinary-looking name, check whether that name is a keyword.
The 51 Reserved Keywords, Grouped
Java 25 has 51 reserved keywords. You don't need to memorize them as a list. Grouped by job, they're easy to recognize:
| Group | Keywords | You'll learn them in |
|---|---|---|
| Data types | byte short int long float double char boolean void | Java Language Basics |
| Flow control | if else switch case default for while do break continue return | Control Statements |
| Classes and objects | class interface enum extends implements new this super instanceof package import | Levels 2 and 3 |
| Access and modifiers | public private protected static final abstract synchronized volatile transient native strictfp | OOP chapters, and multithreading later |
| Errors | try catch finally throw throws assert | Exceptions, later in the course |
| Reserved, not used | const goto | Never given a job; just blocked |
| Special | _ (underscore) | Since Java 22, marks an unnamed variable you don't use (pattern matching, later in the course) |
const and goto were reserved in Java 1.0 so that C and C++ programmers would get a clear error if they used them. Java has never given them a job.
Contextual Keywords: Special Only in Some Places
Newer versions of Java added words such as var, record, yield, sealed and permits. If they had become full keywords, old programs that used them as names would have stopped compiling. So they are contextual keywords: special only in certain positions, and ordinary names everywhere else.
public class KeywordCheck {
public static void main(String[] args) {
String var = "UPI";
int record = 3;
System.out.println(record + " payments by " + var);
}
}3 payments by UPIHow it works
- Line 3 uses
varas a variable name. That's allowed, becausevaris only special where a type is expected, as invar count = 10;. - Line 4 does the same with
record, which is only special when it starts a record declaration. - Line 5 prints both.
It compiles, but it's confusing to read. Treat contextual keywords as off-limits for names in your own code.
Try it yourself: change line 4 to int class = 3; and compile. Which error do you get?
Not Keywords: true, false and null
true, false and null look like keywords, and you can't use them as names either. But officially they're literals: fixed values, like 42 or "Pune". The lesson "Literals" covers them. Interviewers sometimes ask this as a trick question.
Common Mistakes
Using a keyword as a name. Names like new, class, default and package feel natural for variables, but they're keywords. Use newPrice, className, defaultCity.
Capitalizing keywords. Public class Bill or Int count fail to compile. Every keyword is lowercase.
Thinking String and System are keywords. They're class names from the standard library. That's why they start with a capital letter.
Practice
- Easy: Which of these are keywords:
int,Integer,main,static,String,void,null? - Harder: Write a program with variables named
var,recordandyield, and print them. Then trygoto. What does the compiler say?
Key Takeaways
Was this tutorial helpful?