Identifiers & naming conventions in Java
Legal names, case sensitivity, camelCase, PascalCase, UPPER_SNAKE_CASE.
2ndPlace is illegal. $price is fine. And max-size means something totally different. Let's learn Java's naming rules.Rules the compiler enforces
An identifier is a name you choose for a class, method or variable. It may contain letters, digits, _ and $, but it can't start with a digit, can't contain spaces or -, and can't be a keyword.
int score2 = 1; // ok
int _count = 0; // ok
int $price = 5; // ok, but rare
int 2ndPlace = 2; // error: digit firstYour turn
Two variables, one letter of difference. What does this print?
int total = 5;
int Total = 10;
System.out.println(total + Total);15Compile error20
Show the answer
15. Java is case-sensitive: total and Total are two completely separate variables, 5 and 10.
The hyphen trap
max-size isn't a name. Java reads it as max minus size, so the declaration doesn't compile. Use maxSize instead. ($ is legal, but it's normally left to code generated by tools.)
int max-size = 10; // error
int maxSize = 10; // okConventions: the team agreement
Beyond the rules, the Java world agrees on conventions: classes in PascalCase, methods and variables in camelCase, constants in UPPER_SNAKE_CASE, packages in lowercase.
class OrderService { }
int totalPrice;
void sendEmail() { }
static final int MAX_SIZE = 10;
package com.shop.cart;Does javac care?
class orderService and static final int maxRetries = 3; break the conventions. Do they compile?
Think about it, then reveal the answer
Yes, they compile. Conventions aren't compiler rules. But a reviewer will ask for OrderService and MAX_RETRIES, because consistent names let readers tell classes, variables and constants apart at a glance.
In real projects
Big codebases have millions of names. Style checkers like Checkstyle flag naming slips automatically, and reviewers expect conventions to be followed so anyone can read anyone else's code.
Key takeaways
- Can't start with a digit, can't contain - or spaces
- Case-sensitive: total and Total are different names
- Classes PascalCase, methods/variables camelCase
- Constants UPPER_SNAKE_CASE, packages lowercase
Identifiers can use Unicode letters: double π = 3.14159; and int café = 1; are perfectly legal Java.
Practice questions
Which of these is NOT a legal identifier?
- $price
- _count
- 2ndPlace
- firstName2
Check your answer
2ndPlace. Identifiers can contain digits but cannot start with one. $ and _ are allowed (although $ is normally left to generated code).
A code reviewer flags `static final int maxRetries = 3;` and `class orderService`. What's the problem?
- They don't compile: constants must be written in capitals
- They don't compile: class names must start with a capital letter
- javac prints a warning for each badly named identifier
- They compile, but break conventions: use MAX_RETRIES and OrderService
Check your answer
They compile, but break conventions: use MAX_RETRIES and OrderService. Naming conventions are a team agreement, not a compiler rule. The code compiles, but readers expect constants in UPPER_SNAKE_CASE and classes in PascalCase.