Learn Java
import com.learnjava.gettingstarted.KeywordsIdentifiers;

Keywords and Identifiers in Java

@Concept

A keyword is a word the compiler has permanently reserved — class, public, static, if, return, and roughly fifty others always mean the same fixed thing and can never be repurposed as a name. An identifier is the opposite: a name you invent for a class, variable, method, or package. Identifiers must start with a letter, underscore, or dollar sign, and can only contain letters, digits, underscores, and dollar signs after that.

Identifiers are case-sensitive, so total, Total, and TOTAL are three completely distinct names as far as the compiler is concerned. A handful of words — var, record, yield, sealed — are "contextual" keywords: they carry special meaning only in specific positions, and remain legal identifiers everywhere else, unlike true reserved keywords which are off-limits everywhere.

@HowItWorks
1

What counts as a keyword

A fixed, compiler-reserved word — class, if, while, private, void, and similar — that always means the same thing and can never be reused as a variable or class name.

2

What counts as an identifier

Any name you invent: variables, methods, classes, packages. It must start with a letter, underscore, or dollar sign, and contain only letters, digits, underscores, and dollar signs after that.

3

Case sensitivity

count, Count, and COUNT are three distinct identifiers to the compiler — capitalization is never ignored.

4

Contextual keywords

A small set of words like var, record, yield, and sealed act like keywords only in specific spots, and are still legal identifiers everywhere else — unlike true reserved keywords.

5

Convention versus rule

PascalCase for classes, camelCase for variables and methods, and SCREAMING_SNAKE_CASE for constants are strong community conventions that the compiler itself never actually enforces.

@Example
public class NamingDemo {
static final int MAX_USERS = 100;
 
public static void main(String[] args) {
int userCount = 42;
int UserCount = 7;
System.out.println(userCount + " vs " + UserCount);
System.out.println("Limit: " + MAX_USERS);
}
}
Output
42 vs 7
Limit: 100
1class, static, final, int, and public are all reserved keywords — none of them could be reused as a variable name.
2userCount and UserCount are two separate identifiers because Java is case-sensitive, so they hold independent values.
3MAX_USERS follows the constant-naming convention (SCREAMING_SNAKE_CASE) to visually flag that it's declared final.
4The two println calls each print one line, showing all three identifiers — userCount, UserCount, and MAX_USERS — still hold distinct values.
@UseCases
Spotting a 'cannot find symbol' compiler error caused by a typo'd identifier
Understanding why a variable literally named class or int gets rejected by the compiler
Reading style guides that mandate PascalCase, camelCase, or SCREAMING_SNAKE_CASE
Migrating older code where a word like var was a safe identifier but now carries special meaning

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.