Ora

What is a Valid Java Identifier?

Published in Java Naming Conventions 4 mins read

A valid Java identifier is a name used to identify various programming elements such as variables, methods, classes, packages, and interfaces. Adhering to specific rules ensures that your code is syntactically correct and can be compiled successfully by the Java Virtual Machine.

Core Rules for Java Identifiers

Java identifiers must follow a strict set of rules to be considered valid. These rules determine what characters can be used and where:

  1. First Character: The very first character of a Java identifier must be one of the following:

    • A letter: This includes a-z, A-Z, and a wide range of Unicode letters.
    • The dollar sign ($).
    • The underscore (_).

    Examples of valid first characters: myVariable, _counter, $value.
    Examples of invalid first characters: 1stVariable, -item, #define.

  2. Subsequent Characters: Following the first character, any subsequent character in the identifier can be:

    • A letter (as defined above).
    • A digit: This includes 0-9 and various Unicode digits.
    • The dollar sign ($).
    • The underscore (_).

    Examples of valid subsequent characters: myVariable1, _counter_2, final$Value.

Reserved Keywords and Literals

It's crucial to remember that Java has a set of reserved keywords and reserved literals that cannot be used as identifiers. These words have special meanings in the language.

  • Reserved Keywords: Words like public, class, static, void, int, if, else, for, while, return, etc., are reserved. There are 50 such keywords in Java, including const and goto which are reserved but currently unused. You can find a comprehensive list in the Java Language Specification.
  • Reserved Literals: The values true, false, and null are also reserved and cannot be used as identifiers.

Case Sensitivity

Java is a case-sensitive language. This means that myVariable, MyVariable, and myvariable are treated as three distinct identifiers. Always be consistent with your capitalization.

Unicode Support and Length

Java identifiers fully support Unicode characters, allowing you to use letters and digits from various international languages. While there is technically no maximum length for an identifier, it's a best practice to keep names reasonably concise and descriptive for readability.

Best Practices for Naming Identifiers

While the rules define what can be an identifier, industry best practices and conventions dictate what should be used to make code more readable and maintainable. Following these conventions, primarily from Oracle's Java coding standards, is highly recommended.

  • Classes and Interfaces: Use nouns, start with an uppercase letter, and use CamelCase (e.g., MyClassName, Runnable).
  • Methods: Use verbs, start with a lowercase letter, and use camelCase (e.g., calculateSum(), printReport()).
  • Variables: Use nouns, start with a lowercase letter, and use camelCase (e.g., count, firstName).
  • Constants: Use all uppercase letters, with words separated by underscores (snake_case) (e.g., MAX_VALUE, PI).
  • Packages: Use all lowercase letters, typically domain-name reversed (e.g., com.example.myapp).
  • Avoid $: While legally allowed, the dollar sign ($) is conventionally used for synthetic names generated by compilers (e.g., inner classes). Avoid using it in your own code unless specifically necessary.
  • Avoid _: The underscore (_) has specific semantic meaning in some contexts (e.g., in numeric literals for readability, 1_000_000). While permitted for identifiers, excessive use can reduce readability. A single underscore _ can be used as an identifier for an unnamed pattern variable in recent Java versions, though it's not a general-purpose identifier.

Examples of Valid and Invalid Java Identifiers

To clarify the rules, consider the following examples:

Identifier Validity Reason
myVariable Valid Starts with a letter, contains only letters.
_counter Valid Starts with an underscore, contains only letters.
$balance Valid Starts with a dollar sign, contains only letters.
user_ID Valid Starts with a letter, contains letters and an underscore.
totalAmount1 Valid Starts with a letter, contains letters and a digit.
public Invalid public is a reserved keyword.
1stName Invalid Starts with a digit.
my-variable Invalid Contains a hyphen (-), which is not allowed.
has space Invalid Contains a space, which is not allowed.
for Invalid for is a reserved keyword.
my&item Invalid Contains an ampersand (&), which is not allowed.
int Invalid int is a reserved keyword.
void Invalid void is a reserved keyword.
true Invalid true is a reserved literal.
variable$ Valid Dollar sign is allowed anywhere except as the very first character (in this example, it's not the first).

Understanding and applying these rules is fundamental to writing correct and maintainable Java code.