Skip to main content

Java Variable Naming

Naming variables correctly is important for writing readable and maintainable code. Variable names can be enforced by the Java compiler, to make sure they follow the language's rules (no spaces, valid characters etc.), but naming conventions are also used as a guide for developers to structure names so that it remains clear and readable for everyone involved


On this page you’ll learn:​

  • The standard Java naming conventions
  • The difference between compiler-enforced rules and agreed-upon style conventions
  • Examples of good vs. bad variable names and why conventions matter

Variable Naming Rules (Compiler-Enforced)​

In Java, every

must follow certain rules.

What variable names should be​

  • Made up of letters, digits, underscores (_), or dollar signs ($)
  • Begin with a letter, underscore, or dollar sign (never a digit)
  • Written consistently — remember, Java is case-sensitive (age and Age are different)

What variable names cannot be​

  • Cannot start with a digit (2items is invalid)
  • Cannot use spaces or special characters (total-cost, user@name)
  • Cannot be a reserved (class, if, static).
    See the keyword glossary entry for the full list.

Examples​

Valid:

int count;
double _value;
float $price;

Invalid:

int 2count;
double class;
float total-cost;

Variable Naming Conventions (Style Guidelines)​

Java has widely accepted naming conventions, these are not enforced by the compiler but are expected in professional code.