skip to content

What are the `goto` and `const` keywords in Java, and why are they reserved if they do nothing?

level: middleimportance: should knowfreq 45%

answer

  1. goto = unconditional jump, rejected (spaghetti code)
  2. const = constant, rejected (Java uses final)
  3. Reserved but unused / no behavior
  4. Reasons: block C/C++ habits + clearer errors
  5. Labeled break/continue replace goto's valid uses

basics

~20 s

goto and const are reserved words in Java but they have no use — you cannot write working code with them. Java reserved them so they can't be variable names and to leave the door open and give clearer errors.

solid answer

~40 s

Java reserves `goto` and `const` as keywords even though the language gives them no behavior. They are 'reserved but unused.' `goto` (an unconditional jump in C/C++) was deliberately left out because it leads to tangled 'spaghetti' control flow; `const` (a compile-time constant declaration in C/C++) was left out because Java uses `final` instead. They were still reserved for two reasons: so programmers coming from C/C++ can't accidentally use them as identifiers and get subtly wrong behavior, and so the compiler can emit a clear, specific error ('goto not supported') rather than a confusing one. Because they're reserved, `int goto = 1;` is a compile error just like any real keyword.

code

java · 13 lines
java
// Both are compile errors — reserved, even though unused:
// int goto = 5;
// final boolean const = true;

// What Java offers instead of goto: labeled break
outer:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i + j == 2) break outer; // structured 'jump' out of both loops
    }
}
// Instead of const, use final:
final int MAX = 100;

go deeper

for a junior

Knows goto and const exist as reserved words but do nothing in Java and can't be used as names.

for a middle

Can explain why they were reserved without behavior (block C/C++ misuse, clearer errors) and name the replacements (labeled break/continue, final).

for a senior

Distinguishes 'reserved' from 'has semantics' and discusses forward-compatibility (room to add later) as a design rationale.

for a principal

Frames it as language-evolution policy: hard-reserving unused tokens to preserve future grammar space and avoid breaking changes, contrasted with the contextual-keyword strategy used for newer features.

## Background: what these words mean elsewhere In **C and C++**, two of these words are real, working features: - **`goto`** is an *unconditional jump* — it transfers control to a labeled line anywhere in the function. It is powerful but notorious for producing **'spaghetti code'**: control flow that jumps around unpredictably and is hard to read or reason about. - **`const`** declares a *constant* — a value that cannot change after initialization (similar in spirit to Java's `final`). ## What Java did Java's designers chose **not to implement** either feature: - They rejected `goto` on principle, favoring structured control flow (`if`/`for`/`while`/`break`/`continue`, including **labeled** `break`/`continue` which cover the rare legitimate uses of `goto`). - They rejected `const` because Java already has **`final`** to mark variables, fields, parameters, methods, and classes as unchangeable. ## Why reserve a word you won't use? This is the key insight. A word can be **reserved** (forbidden as an identifier) without having any behavior. Java did this for `goto` and `const` for two reasons: 1. **Migration safety.** Millions of C/C++ programmers were expected to move to Java. If `goto` were a legal identifier, someone might write `int goto = label;` thinking it does something, and the code would compile with a meaning they never intended. Reserving the word makes that **impossible**, surfacing a mistake immediately. 2. **Better error messages.** Because the compiler *knows* `goto` is reserved, it can say exactly: 'goto is reserved but not supported' instead of a vague parse error. This guides the developer to the structured alternative. ## How this looks in practice `int goto = 5;` and `boolean const = true;` are both **compile errors** — the words are off-limits as names, identical in that respect to `class` or `for`. But you can never write a statement that *uses* `goto` or `const` to do anything, because the language defines no semantics for them. ## The deeper lesson This demonstrates that 'reserved word' and 'keyword with behavior' are **different concepts**. A reserved word is anything the grammar forbids as an identifier; most also carry meaning, but `goto`/`const` are reserved with **no** meaning. It's a defensive, forward-compatible design choice — leaving room to add the feature later without breaking existing code (in practice Java has never used them).

  • How does Java cover the legitimate uses of goto without goto?
    With structured control flow plus labeled `break` and `continue`, which let you jump out of or continue an outer loop by name — covering the rare cases where C programmers reach for goto.
  • What does Java use instead of const?
    `final` — it marks variables, fields, parameters, methods, or classes as unchangeable. A `static final` field is the usual way to declare a compile-time constant.

Like a hotel that keeps a room permanently locked and labeled 'out of service' — it's reserved (you can't book it) even though nobody uses it, so guests don't wander in expecting a bed.

saying these in an interview costs you the question

  • Saying goto works in Java if you add a label (it does not — labels exist only for break/continue)
  • Claiming const is just an alias for final in Java (const has no behavior at all; final is the real keyword)
  • Thinking reserved-but-unused words can be used as identifiers (they cannot — reservation still applies)

context