Java Keywords

Naveen AnamaneniUpdated · 5 min read

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:

Compiler output
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:

Java's 51 reserved keywords, grouped by job
GroupKeywordsYou'll learn them in
Data typesbyte short int long float double char boolean voidJava Language Basics
Flow controlif else switch case default for while do break continue returnControl Statements
Classes and objectsclass interface enum extends implements new this super instanceof package importLevels 2 and 3
Access and modifierspublic private protected static final abstract synchronized volatile transient native strictfpOOP chapters, and multithreading later
Errorstry catch finally throw throws assertExceptions, later in the course
Reserved, not usedconst gotoNever 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.

KeywordCheck.java▶ Try it
public class KeywordCheck {
    public static void main(String[] args) {
        String var = "UPI";
        int record = 3;
        System.out.println(record + " payments by " + var);
    }
}
Output
3 payments by UPI

How it works

  1. Line 3 uses var as a variable name. That's allowed, because var is only special where a type is expected, as in var count = 10;.
  2. Line 4 does the same with record, which is only special when it starts a record declaration.
  3. 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

  1. Easy: Which of these are keywords: int, Integer, main, static, String, void, null?
  2. Harder: Write a program with variables named var, record and yield, and print them. Then try goto. What does the compiler say?

Quick check: Java keywords

4 questions
1How many reserved keywords does Java 25 have?
2Which of these is NOT a keyword?
3Why can var still be used as a variable name?
4What are true, false and null?

Key Takeaways

Was this tutorial helpful?

Lesson 27 of 482