Java Identifiers

Identifiers are programmer-defined names used for variables, methods, classes, packages, and interfaces in Java. They make the program more readable and meaningful.


  • Identifiers are programmer-defined tokens used to give names to program elements.

    Examples include names of variables, methods, classes, and interfaces.

Rules for a Legal Identifier

  1. They can contain alphabets, digits, underscore (_) and dollar ($) characters.
  2. They must not begin with a digit.
  3. Java is case-sensitive, so uppercase and lowercase letters are distinct.
  4. They can be of any length.
  5. They must not be a Java keyword.

Examples of Legal Identifiers:
arena
s_count
marks40
class_one
Examples of Illegal Identifiers:
1sst        // starts with a digit
2nd number  // contains a space
oh!god      // contains illegal symbol '!' 

Always choose meaningful names for identifiers, following camelCase or PascalCase conventions for better readability.


Java Naming Conventions

  • πŸ”Ή Variables: camelCase (e.g., studentAge, totalMarks)
  • πŸ”Ή Methods: camelCase (e.g., getStudentName(), calculateTotal())
  • πŸ”Ή Classes / Interfaces: PascalCase (e.g., StudentRecord, EmployeeDetails)
  • πŸ”Ή Constants: UPPER_CASE with underscores (e.g., MAX_VALUE, PI)
  • πŸ”Ή Packages: lowercase (e.g., com.prowessapps.java)

Summary Table

Identifier Type Convention Example
Variable camelCase studentAge
Method camelCase getStudentName()
Class / Interface PascalCase StudentRecord
Constant UPPER_CASE MAX_VALUE
Package lowercase com.prowessapps.java

Best Practices for Naming
  • βœ… Always use meaningful and descriptive names (e.g., studentAge instead of x).
  • βœ… Maintain consistency across the entire project.
  • βœ… Avoid abbreviations unless they are widely understood (e.g., HTMLParser).
  • βœ… Never start identifiers with underscores or dollar signs (except in special cases like generated code).
  • βœ… Keep class names as nouns, method names as verbs.
  • βœ… Follow Java community standards so your code looks professional.

πŸ“₯ Download the Java Identifiers & Naming Conventions Cheatsheet (PDF)

Next: Java Literals




πŸš€ Quick Knowledge Check

Topic: Identifiers | Language: Java

Q1. Which of the following is a valid identifier in Java?
Q2. Java identifiers can contain special characters like @ or #.
Q3. Is the following identifier legal or illegal? $amount
Q4. Which of the following identifiers is valid?
Q5. An identifier in Java cannot start with a _____.
Q6. Which of the following is a recommended identifier name for a class?
Q7. Why should you avoid using $ or _ in identifier names?
Q8. Which naming convention is recommended for variable names in Java?
Q9. Which of the following identifiers is illegal?
Q10. Identifiers in Java are case-sensitive.