Java Identifiers - Rules, Examples and Naming Conventions

⏱️ 6 min read • Beginner Level • Lesson 12

Lesson 12 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

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

Before learning identifiers, you should understand Java keywords and variables in Java, because identifiers are commonly used to name variables, methods, and classes.


What are Java Identifiers?

In Java, an identifier is a name given by the programmer to identify program elements such as variables, classes, methods, interfaces, packages, and objects.

Real-Life Example

Think of identifiers like names assigned to people. Instead of calling someone "Person 1" or "Person 2", we use names such as John or Sarah. Similarly, in Java we use identifiers such as studentAge or totalMarks to identify data and program elements.

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

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

  • Good identifiers improve readability and make code easier to understand and maintain.

Rules for Java Identifiers

Java identifiers must follow certain rules. If these rules are not followed, the compiler will show an error.

  1. Identifiers can contain alphabets, digits, underscore (_), and dollar sign ($).
  2. Identifiers must not begin with a digit.
  3. Java is case-sensitive, so age, Age, and AGE are different identifiers.
  4. Identifiers can be of any length.
  5. Identifiers must not be a Java keyword, such as class, public, static, or int.
Note: Although $ and _ are allowed in identifiers, they should generally be avoided in normal variable and method names unless required.

These identifiers are valid because they follow Java identifier rules.

LegalIdentifiers.java
Copy Try Download
public class LegalIdentifiers {
    public static void main(String[] args) {
        int studentAge = 20;
        int marks40 = 85;
        int s_count = 10;
        int totalMarks = 450;

        System.out.println(studentAge);
        System.out.println(marks40);
        System.out.println(s_count);
        System.out.println(totalMarks);
    }
}
Explanation:
  • studentAge follows camelCase naming convention.
  • marks40 contains digits but does not start with one.
  • s_count uses an underscore.
  • All identifiers follow Java naming rules.

Examples of Illegal Identifiers

These identifiers are invalid because they break Java naming rules.

IllegalIdentifiers.java
Copy Download
int 1stNumber;     // Invalid: starts with a digit
int student age;   // Invalid: contains a space
int oh!god;        // Invalid: contains illegal symbol !
int class;         // Invalid: class is a Java keyword
Why these are invalid:
  • 1stNumber starts with a digit.
  • student age contains a space.
  • oh!god contains an invalid character.
  • class is a reserved Java keyword.

Always choose meaningful names for identifiers. For example, use studentAge instead of x.


Java Naming Conventions

Naming conventions are not mandatory rules, but they are strongly recommended because they make Java code professional and easier to read.

  • Variables: use camelCase (for example, studentAge, totalMarks).
  • Methods: use camelCase (for example, getStudentName(), calculateTotal()).
  • Classes / Interfaces: use PascalCase (for example, StudentRecord, EmployeeDetails).
  • Constants: use UPPER_CASE with underscores (for example, MAX_VALUE, PI).
  • Packages: use lowercase (for example, com.prowessapps.java).

Java Identifier Naming Summary

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

Common Mistakes

  • Starting identifiers with numbers.
  • Using spaces in identifiers.
  • Using Java keywords as identifiers.
  • Using unclear names such as x, a1, temp.
  • Ignoring naming conventions.

Best Practices for Naming Identifiers

  • Use meaningful and descriptive names, such as studentAge instead of x.
  • Maintain consistency across the entire project.
  • Avoid abbreviations unless they are widely understood.
  • Avoid starting identifiers with underscores or dollar signs in normal code.
  • Keep class names as nouns and method names as verbs.
  • Follow Java community standards so your code looks professional.

📥 Download the Java Identifiers and Naming Conventions Cheatsheet (PDF)

Summary:
  • Identifiers are names given to variables, methods, classes, packages, and interfaces.
  • Java identifiers can contain letters, digits, underscore, and dollar sign.
  • Identifiers cannot start with a digit and cannot be Java keywords.
  • Java is case-sensitive, so age and Age are different.
  • Using naming conventions improves readability and code quality.

Interview Questions ⭐

Identifiers in Java are programmer-defined names used for variables, methods, classes, interfaces, packages, and other program elements.

Java identifiers can contain letters, digits, underscore, and dollar sign, but they cannot start with a digit and cannot be Java keywords.

No, Java identifiers cannot start with a number. For example, 1student is invalid, but student1 is valid.

Yes, Java identifiers are case-sensitive. For example, age, Age, and AGE are treated as different identifiers.

No, Java keywords such as class, public, static, int, and return cannot be used as identifiers.

Examples of valid Java identifiers include studentAge, totalMarks, marks40, and student_name.

Examples of invalid identifiers include 1number, student name, class, and total-marks because they break Java identifier rules.

Java variables usually follow camelCase naming convention, such as studentAge, totalMarks, and userName.

Java classes usually follow PascalCase naming convention, such as StudentRecord, EmployeeDetails, and BankAccount.

Meaningful identifiers make Java programs easier to read, understand, debug, and maintain.

Next step: Learn Java Naming Conventions

🚀 Continue to Java Naming Conventions →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Identifiers | Language: Java

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

🎉 Great job! Continue learning Java step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.