Java Identifiers

3 min read ·

In Java, an identifier is the name given to program elements such as:
  • Variables
  • Methods
  • Classes
  • Interfaces
  • Packages
Whenever you define a name in Java, that name is called an identifier.
Example:
In this program:
  • Main is a class identifier
  • main is a method identifier
  • age is a variable identifier

Rules for Java Identifiers

Java has strict rules for naming identifiers. If these rules are violated, the program will not compile.

1. Allowed Characters

Identifiers can contain:
  • Letters a to z and A to Z
  • Digits 0 to 9
  • Underscore _
  • Dollar sign $
Example:
This program runs successfully because all identifiers follow valid naming rules.

2. Cannot Start With a Digit

An identifier cannot begin with a number.
Incorrect:
Correct:
Digits are allowed inside the name, but not at the beginning.

3. Cannot Use Java Keywords

Keywords are reserved words in Java and cannot be used as identifiers.
Examples of keywords:
  • int
  • class
  • public
  • static
  • void
Incorrect:
Correct:

4. Case Sensitive

Java is case sensitive, meaning uppercase and lowercase letters are treated differently.
Output:
10 20
Both variables are different because of case difference.

5. No Spaces Allowed

Identifiers cannot contain spaces.
Incorrect:
Correct:
Camel case naming is used instead of spaces.

Identifier Naming Conventions

Following naming conventions improves readability and professionalism.

Variable and Method Naming

  • Use camelCase
  • Start with lowercase letter
Example:

Class Naming

  • Start with uppercase letter
  • Use PascalCase
Example:

Every name that you define in Java is an identifier. Understanding identifier rules is essential because they form the foundation of writing correct and professional Java programs.