Identifiers & Keywords in Java
Rules for naming identifiers, list of reserved keywords, and Java naming conventions.
Learning objectives
- βWrite identifiers that follow Java rules
- βRecognise the reserved keywords
- βApply the standard naming conventions
π‘ Key points
- Identifier = name of a class, method, variable, package, or label.
- Rules: start with letter / _ / $; then any letters or digits; no keywords; case-sensitive.
- Java has 50+ reserved words β they cannot be used as identifiers.
- Convention: classes PascalCase, methods/vars camelCase, constants SCREAMING_SNAKE_CASE, packages lowercase.dot.form.
π» Code examples(2)
// valid
int age;
String userName;
double _total;
long $money;
int itemCount1;
// invalid β will not compile
// int 1item; // starts with digit
// int user-name; // hyphen not allowed
// int class; // reserved keyword
public class BankAccount { // Class β PascalCase
static final double MIN_BALANCE = 500.0; // constant
private String accountHolder; // instance var β camelCase
public double calculateInterest() { // method β camelCase
return accountHolder != null ? 0.05 : 0.0;
}
}
π― Practice
Q1. Is `2ndPlayer` a valid Java identifier? Why?+
No. Identifiers cannot start with a digit.
Q2. Which of these are keywords: class, Class, static, String?+
class and static are reserved keywords. Class and String are classes (identifiers), not keywords.
Q3. Rename `int TOTAL_score` to follow Java conventions.+
int totalScore β variables use camelCase; SCREAMING_SNAKE_CASE is reserved for constants (final).
π Notes
Reserved keywords (partial list)
abstract, assert, boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum, extends, final, finally, float, for, goto, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while.
true, false, null are literals β also cannot be used as identifiers.
Quick tips
- Choose meaningful names β
emailAddressbeatsea. - Avoid single letters except for loop counters (
i,j,k). - No length limit, but keep names readable.