Course contentsShow
← Java
Lesson 682 of 2,87016. Packages, Imports, and JAR FilesFree lesson

Avoiding Reserved Keywords in Package Names

Learn restrictions on using Java keywords and how to handle them in package names.

Avoiding Reserved Keywords in Package Names

You'll learn why Java's reserved keywords cannot appear in package names and how to handle naming conflicts when they do.

The Core Restriction

Java has a set of reserved keywords like class, int, if, while, package, import, and dozens more. These words have special meaning to the compiler and cannot be used as identifiers—including in package names.

If you tried to create a package named com.example.class or org.package.utils, the compiler would reject it immediately. The keyword collides with Java's syntax rules.

Why This Matters

Package names become part of your directory structure and are used throughout your code. Since packages translate directly to folders and affect fully qualified names, using a keyword would create ambiguity:

// ILLEGAL - "class" is a reserved keyword
package com.example.class;  // ❌ Compile error

// LEGAL - renamed to avoid conflict
package com.example.clazz;  // ✓ Works

Common Workarounds

When a natural package name would conflict with a keyword, developers typically:

  • Add a suffix: classutils, packageinfo
  • Use abbreviations: clazz, pkg
  • Rephrase: Instead of com.example.interface, use com.example.api or com.example.contracts

Think of reserved keywords like restricted airspace—you simply can't build there, so you choose nearby, available "land."

Real-World Context

This rarely causes problems in practice because reverse domain naming (com.mycompany.myapp) naturally avoids keywords. But if your domain or application name happens to match a keyword, planning ahead prevents compilation errors.

Key Takeaway: Java reserved keywords cannot be used in package names; rename them using suffixes, abbreviations, or synonyms to avoid compiler errors.