Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Identifiers in Java

An identifier is the name you give to a variable, method, class, interface, or package in Java. Choosing identifiers correctly involves two separate things: the hard rules the compiler enforces, and the naming conventions the Java community follows as best practice — breaking a rule causes a compile error, but breaking a convention just makes code harder to read.

Why it matters

Names are the part of a program people read most. The compiler only enforces a few hard rules, but the naming conventions on top of them are what make Java code from different teams look familiar: you can tell a class from a variable from a constant at a glance, without checking its declaration.

Watch: How Java Evaluates Identifiers

A quick video on how Java decides whether a name is a valid identifier.

Watch on YouTube

Compiler Rules for Valid Identifiers

These rules are enforced by the compiler; violating any of them prevents the code from compiling at all.

  • An identifier can contain letters, digits, underscores (_), and dollar signs ($) — no other characters (spaces, hyphens, @ symbols, etc.) are allowed.
  • It cannot start with a digit, though it can contain digits after the first character (e.g. score2 is valid, 2ndScore is not).
  • It cannot be a reserved keyword (such as class, int, or static) or a reserved literal (true, false, null).
  • Java identifiers are case-sensitive, so total, Total, and TOTAL are three completely different identifiers.

Naming Conventions (Best Practice, Not Enforced)

Conventions are not checked by the compiler, but ignoring them makes code confusing to other Java developers and to tools like IDEs that assume standard style. Classes and interfaces use UpperCamelCase (StudentRecord), while variables and methods use lowerCamelCase (studentName, calculateTotal()). Constants (fields declared static final) are written in all uppercase with underscores separating words (MAX_DISCOUNT). Package names are written entirely in lowercase (com.webnest.utils). The underscore and dollar sign are technically legal in any identifier, but by convention they are avoided in ordinary variable names — the dollar sign is mostly reserved for tool-generated or compiler-generated code.

Example

Valid identifiers, including edge cases with underscore and dollar sign

Java
public class IdentifierDemo {
    public static void main(String[] args) {
        int _count = 10;
        double $price = 19.99;
        String studentName = "Asha";
        int number2 = 5;

        System.out.println(_count);
        System.out.println($price);
        System.out.println(studentName);
        System.out.println(number2);
    }
}
Output
10
19.99
Asha
5

Common Mistakes

  • Trying to start an identifier with a digit, such as "2ndPlace" — this fails to compile.
  • Using a reserved keyword like "class" or "int" as a variable name.
  • Assuming identifiers are case-insensitive and being surprised that "Score" and "score" refer to two different variables.
  • Mixing naming conventions inconsistently (e.g. naming a class "studentRecord" instead of "StudentRecord"), which compiles fine but confuses readers.

Exercise

This program does not compile because three names break the identifier rules, and one more breaks the convention for constants. Rename them so the program compiles and follows Java conventions.

Expected output: Total: 250

Java · starter code
public class Checkout {
    static final int maxitems = 10;       // convention problem

    public static void main(String[] args) {
        int 2ndPrice = 150;               // rule problem
        int first-price = 100;            // rule problem
        int class = 2ndPrice + first-price; // rule problem
        System.out.println("Total: " + class);
    }
}
Show hints
  • An identifier cannot start with a digit, cannot contain a hyphen, and cannot be a reserved keyword.
  • Constants are written in upper case with underscores, and variables in camelCase.
Show solution
Java · solution
public class Checkout {
    static final int MAX_ITEMS = 10;

    public static void main(String[] args) {
        int secondPrice = 150;
        int firstPrice = 100;
        int total = secondPrice + firstPrice;
        System.out.println("Total: " + total);
    }
}

Quick quiz

1. Which of these is a valid Java identifier?

  • A. 2ndPlace
  • B. total-price
  • C. _count
  • D. class
Show answer

C. _count is valid. An identifier cannot start with a digit, cannot contain a hyphen, and cannot be a reserved keyword.

2. Are total and Total the same identifier?

  • A. Yes, Java ignores case
  • B. Only for variables, not for methods
  • C. Only inside the same method
  • D. No, Java is case-sensitive
Show answer

D. Java is case-sensitive, so these are two different names.

3. Which name follows the Java convention for a constant?

  • A. MAX_SIZE
  • B. MaxSize
  • C. maxSize
  • D. max_size
Show answer

A. Constants (static final fields) are written in upper case with underscores between words.

Interview questions

What is the difference between identifier rules and naming conventions?

Show answer

Rules are enforced by the compiler: an identifier may contain letters, digits, underscore and dollar sign, cannot start with a digit, and cannot be a reserved word. Breaking a rule is a compile error.

Conventions are agreed style that the compiler does not check: PascalCase for classes, camelCase for methods and variables, UPPER_SNAKE_CASE for constants, and lower-case package names. Breaking a convention still compiles, but makes code harder for other Java developers to read.

Key Points to Remember

  • Identifiers may contain letters, digits, underscores, and dollar signs, but cannot start with a digit.
  • Identifiers cannot be reserved keywords or reserved literals (true, false, null).
  • Java identifiers are case-sensitive.
  • Naming conventions (UpperCamelCase for classes, lowerCamelCase for variables/methods, UPPER_CASE for constants) are best practice, not compiler rules.

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering