Java Identifiers

Identifiers are one of the most basic and important concepts in Java programming. Whenever you create a variable, method, class, interface, package, or other named program element, you give it a name. These names are called identifiers.

For example:

int age = 25;

Here, age is an identifier.

Similarly:

class Student {
}

Here, Student is an identifier because it is the name of the class.

Understanding identifiers is important because Java has specific rules about which names are valid and which names are not allowed.

Your First Java Program

What Is an Identifier in Java?

An identifier is a name used to identify a program element in Java.

Identifiers can be used for things such as:

  • Classes
  • Interfaces
  • Methods
  • Variables
  • Objects
  • Packages
  • Parameters
  • Enums
  • Fields

For example:

class Student {

    int age;

    void displayAge() {
        System.out.println(age);
    }
}

There are several identifiers in this example:

  • Student — class identifier
  • age — variable/field identifier
  • displayAge — method identifier

Identifiers allow developers to refer to these elements throughout a program.

Simple Example

Consider:

int salary = 50000;

The parts can be understood as:

int       → data type
salary    → identifier
=         → assignment operator
50000     → value
;         → statement terminator

The identifier salary gives a name to the variable that stores the value 50000.

Why Are Identifiers Important?

Identifiers make programs readable and understandable.

Compare:

int x = 50000;

with:

int monthlySalary = 50000;

Both are valid Java code, but monthlySalary communicates the purpose of the variable much more clearly.

Good identifiers make it easier for developers to understand:

  • What a variable represents
  • What a method does
  • What a class represents
  • What an object contains
  • What a package is used for

Choosing meaningful identifiers is therefore an important part of writing maintainable Java applications.

Rules for Java Identifiers

Java has specific rules that must be followed when creating identifiers.

Identifiers Can Contain Letters

An identifier can contain letters from the Java language’s permitted identifier character set.

For example:

int age = 25;
String name = "John";
double salary = 45000.50;

Here:

age
name
salary

are valid identifiers.

Identifiers Can Contain Digits

Digits can be used in identifiers, but an identifier cannot begin with a digit.

Valid:

int age2 = 25;
int student2026 = 100;

Invalid:

int 2age = 25;

The second example is invalid because the identifier begins with a digit.

A useful rule to remember is:

Digits can be part of an identifier, but they cannot be the first character.

Identifiers Can Contain Underscores

The underscore character _ can be used in Java identifiers.

For example:

int student_age = 22;
int total_marks = 500;

These are valid identifiers.

However, Java developers commonly use camelCase for variables and methods instead of underscores.

For example:

int studentAge = 22;
int totalMarks = 500;

This is generally more consistent with common Java naming conventions.

Identifiers Can Contain the Dollar Sign

The dollar sign $ is technically allowed in Java identifiers.

For example:

int $value = 100;

This is syntactically valid Java.

However, $ is generally not recommended for normal application-level variable or method names. It can make code look unusual, and $ is commonly encountered in compiler-generated or framework-generated names.

For normal Java development, prefer clear names using letters, digits, and conventional naming styles.

Identifiers Cannot Contain Spaces

Spaces are not allowed inside an identifier.

Invalid:

int student age = 20;

The compiler interprets student and age as separate tokens.

Instead, use camelCase:

int studentAge = 20;

Or, technically, an underscore:

int student_age = 20;

For typical Java code, studentAge is the conventional choice.

Identifiers Cannot Start With a Digit

This is one of the most important rules.

Invalid:

int 123number = 10;

Valid:

int number123 = 10;

The difference is that the valid identifier starts with a letter.

Java Identifiers Are Case-Sensitive

Java identifiers are case-sensitive.

That means these are different identifiers:

age
Age
AGE

For example:

int age = 20;
int Age = 30;

Java treats age and Age as two different variable names.

Similarly:

Student
student
STUDENT

are different identifiers.

Although Java technically allows names that differ only by capitalization, using such names together can make code confusing.

Keywords Cannot Normally Be Used as Identifiers

Java has reserved keywords with predefined meanings.

Examples include:

class
public
static
void
int
if
else
for
while
return
new
private
protected

You cannot normally use these keywords as identifiers.

For example:

int class = 10;

is invalid because class is a Java keyword.

Similarly:

int public = 20;

is invalid because public is a keyword.

Instead, choose another name:

int classNumber = 10;

Reserved Literals Cannot Be Used as Identifiers

Java also reserves certain literal values.

For example:

true
false
null

cannot be used as ordinary identifiers.

Invalid:

int true = 10;

Invalid:

String null = "value";

These words have predefined meanings in Java.

Valid and Invalid Identifiers

Here are some examples:

IdentifierValid?Reason
ageYesValid name
studentNameYesValid camelCase identifier
student2026YesDigits are allowed after the first character
_countYesUnderscore is allowed
$valueYes$ is technically permitted
2valueNoCannot start with a digit
student nameNoSpaces are not allowed
classNoJava keyword
publicNoJava keyword
trueNoReserved literal
nullNoReserved literal
total-marksNo- is an operator, not an identifier character

Java Naming Conventions

Java’s syntax rules determine whether an identifier is valid. Naming conventions, on the other hand, describe how developers commonly choose names.

Following naming conventions makes Java code more readable and consistent.

Variable Names

Variables and fields commonly use camelCase.

Examples:

studentName
studentAge
totalMarks
monthlySalary
accountBalance

The first word starts with a lowercase letter, and subsequent words begin with uppercase letters.

For example:

int studentAge = 22;

Method Names

Method names also commonly use camelCase.

Examples:

calculateTotal()
getStudentName()
displayResult()
findUser()
calculateSalary()

For example:

static int calculateTotal(int price, int quantity) {
    return price * quantity;
}

The method name calculateTotal follows camelCase.

Class Names

Class names conventionally use PascalCase, where each word starts with an uppercase letter.

Examples:

Student
Employee
BankAccount
CustomerService
PaymentProcessor

For example:

class Student {
}

Another example:

class BankAccount {
}

Interface Names

Interface names commonly use PascalCase as well.

Examples:

Runnable
Comparable
Serializable
PaymentService
UserRepository

For example:

interface PaymentService {
}

Constants

Constants are commonly written using UPPER_SNAKE_CASE.

For example:

static final int MAX_USERS = 100;

Another example:

static final double PI_VALUE = 3.14159;

Here:

MAX_USERS
PI_VALUE

follow the conventional constant naming style.

Package Names

Package names are conventionally written in lowercase.

For example:

com.example.application

Another example:

com.web4learners.java

Package names are usually organized in reverse-domain style for applications and libraries.

Common Java Naming Styles

A Java project commonly uses these naming styles:

Program ElementCommon ConventionExample
ClassPascalCaseStudentDetails
InterfacePascalCasePaymentService
MethodcamelCasecalculateTotal()
VariablecamelCasestudentAge
ParametercamelCasestudentName
ConstantUPPER_SNAKE_CASEMAX_VALUE
Packagelowercasecom.example.app

These are conventions rather than the basic syntax rules of the language.

Identifiers and Variables

When you declare a variable, the variable’s name is an identifier.

For example:

int age = 25;

Here:

int  → type
age  → identifier
25   → value

You can later use the identifier to access the variable:

System.out.println(age);

The identifier age tells Java which variable you are referring to.

Identifiers and Methods

Method names are also identifiers.

For example:

static void greet() {
    System.out.println("Hello");
}

Here:

greet

is the method identifier.

You can call the method using its identifier:

greet();

Another example:

static int addNumbers(int a, int b) {
    return a + b;
}

Here:

  • addNumbers is the method identifier
  • a is a parameter identifier
  • b is a parameter identifier

Identifiers and Classes

Class names are identifiers.

For example:

class Employee {
}

Employee is the class identifier.

You can create an object using that class:

Employee employee = new Employee();

Here:

  • Employee identifies the class
  • employee identifies the variable that refers to the object

This demonstrates that different program elements can have identifiers.

Identifiers and Objects

When an object is assigned to a variable, the variable name is an identifier.

For example:

Student student = new Student();

Here:

Student → class name
student → variable identifier
new     → keyword

The identifier student can then be used to access the object:

student.display();

Identifiers and Method Parameters

Method parameters also have identifiers.

For example:

static int multiply(int firstNumber, int secondNumber) {
    return firstNumber * secondNumber;
}

Here:

firstNumber
secondNumber

are parameter identifiers.

They allow the method to refer to the values passed into it.

Identifiers and Scope

An identifier is only usable where the corresponding declaration is accessible.

For example:

class Demo {

    public static void main(String[] args) {

        int age = 25;

        System.out.println(age);
    }
}

The identifier age is declared inside main, so it is a local variable within that method.

Trying to use it outside its scope would cause a compilation error.

class Demo {

    public static void main(String[] args) {
        int age = 25;
    }

    static void display() {
        System.out.println(age);
    }
}

This does not compile because age is a local variable belonging to main.

Can Two Identifiers Have the Same Name?

Java allows some identifiers to have the same name when they exist in different scopes or represent different declarations where the language permits it.

For example:

class Student {

    int age;

    void display(int age) {
        System.out.println(age);
    }
}

Here, the field and parameter both have the name age.

The parameter age takes precedence within the method’s scope.

You can explicitly refer to the instance field using this:

class Student {

    int age;

    void setAge(int age) {
        this.age = age;
    }
}

Here:

age       → method parameter
this.age  → instance field

This is called shadowing, where a declaration in a nested scope has the same name as another accessible declaration.

Identifiers Cannot Be Arbitrary Symbols

Characters such as these cannot generally be used within ordinary Java identifiers:

@
#
%
&
*
-
+
=
/

For example:

int total-marks = 500;

is invalid because - is interpreted as the subtraction operator.

Instead:

int totalMarks = 500;

is valid.

Unicode Identifiers

Java supports Unicode characters in identifiers, which means identifiers are not limited to basic English letters.

For example, technically Java can accept identifiers using characters from many writing systems.

However, in most professional Java projects, developers generally prefer clear and portable naming conventions using commonly understood Latin letters, especially when working on international teams.

The key point is that Java’s identifier rules are broader than simply A-Z and a-z.

Identifier vs Keyword

An identifier is a name created by the programmer.

A keyword is a reserved word that has a predefined meaning in Java.

For example:

int age = 25;

Here:

int → keyword
age → identifier

Another example:

class Student {
}

Here:

class   → keyword
Student → identifier

You cannot use int or class as normal identifiers.

Identifier vs Literal

It is also useful to distinguish identifiers from literals.

Consider:

int age = 25;

Here:

int  → keyword
age  → identifier
25   → integer literal

The identifier gives a name to the variable, while the literal represents the actual value.

Another example:

String name = "Karim";

Here:

String → type
name   → identifier
"Karim" → string literal

Choosing Good Identifiers

A good identifier should clearly communicate what the program element represents.

Poor:

int x = 50000;

Better:

int monthlySalary = 50000;

Poor:

void p() {
}

Better:

void printStudentDetails() {
}

Good identifiers reduce the need for unnecessary comments and make programs easier to maintain.

Avoid Extremely Long Identifiers

Meaningful names are good, but identifiers should not become unnecessarily long.

For example:

int numberOfStudentsWhoSuccessfullyCompletedTheJavaProgrammingCourse = 50;

This is technically valid, but unnecessarily verbose.

A clearer alternative might be:

int completedStudents = 50;

The goal is to find a balance between clarity and simplicity.

Avoid Single-Letter Names When Possible

Single-letter identifiers can be appropriate in limited situations, such as loop counters:

for (int i = 0; i < 10; i++) {
    System.out.println(i);
}

Here, i is commonly understood as a loop counter.

However, for important variables, descriptive names are generally better:

int customerCount = 100;

instead of:

int c = 100;

Avoid Confusing Names

Identifiers that differ only by capitalization can be difficult to distinguish.

For example:

int user;
int User;

Although valid in different contexts, this naming style can make code harder to read.

Similarly, avoid names that are visually confusing:

l
I
1
O
0

Clear names improve readability.

Complete Example

The following program demonstrates several types of identifiers:

class StudentDetails {

    static final int MAX_MARKS = 100;

    String studentName;
    int studentAge;

    StudentDetails(String studentName, int studentAge) {
        this.studentName = studentName;
        this.studentAge = studentAge;
    }

    void displayDetails() {
        System.out.println("Name: " + studentName);
        System.out.println("Age: " + studentAge);
    }

    public static void main(String[] args) {

        StudentDetails student = new StudentDetails("Karim", 22);

        student.displayDetails();
    }
}

Some identifiers in this example include:

StudentDetails
MAX_MARKS
studentName
studentAge
displayDetails
student

They represent different types of Java program elements.

Common Identifier Mistakes

Starting With a Number

Incorrect:

int 1student = 10;

Correct:

int student1 = 10;

Using Spaces

Incorrect:

int student name = 10;

Correct:

int studentName = 10;

Using a Keyword

Incorrect:

int class = 10;

Correct:

int classNumber = 10;

Using Special Operators

Incorrect:

int total-marks = 500;

Correct:

int totalMarks = 500;

Ignoring Case Sensitivity

These are different identifiers:

int age = 20;
int Age = 30;

Do not assume Java treats them as the same name.

Frequently Asked Questions

What is an identifier in Java?

An identifier is a name used to identify a Java program element such as a variable, method, class, interface, or package.

Can an identifier start with a number?

No. An identifier cannot begin with a digit.

int 2value = 10; // Invalid

Can an identifier contain numbers?

Yes.

int value2 = 10;

is valid.

Can Java identifiers contain spaces?

No. Spaces are not allowed within identifiers.

Use camelCase instead:

studentName

Are Java identifiers case-sensitive?

Yes. age, Age, and AGE are different identifiers.

Can a Java keyword be used as an identifier?

No. Keywords such as class, public, int, and return cannot normally be used as identifiers.

Can _ be used in an identifier?

Yes.

int student_age = 20;

is valid, although Java code commonly uses camelCase:

int studentAge = 20;

Can $ be used in an identifier?

Yes, $ is technically allowed, but it is generally avoided in normal application code unless there is a specific reason to use it.

Can an identifier contain Unicode characters?

Yes. Java supports Unicode characters in identifiers, subject to Java’s identifier rules.

What naming convention is used for Java variables?

Java variables commonly use camelCase.

studentName
totalMarks
monthlySalary

What naming convention is used for Java classes?

Java classes commonly use PascalCase.

Student
BankAccount
EmployeeDetails

What naming convention is used for Java constants?

Constants commonly use uppercase letters with underscores.

MAX_VALUE
DEFAULT_TIMEOUT
MAX_USERS