☕ Java Foundations · Beginner

Identifiers & naming conventions in Java

Legal names, case sensitivity, camelCase, PascalCase, UPPER_SNAKE_CASE.

🧩 The mystery2ndPlace 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 first
🔮 Predict it

Your turn

Two variables, one letter of difference. What does this print?

int total = 5;
int Total = 10;
System.out.println(total + Total);
  1. 15
  2. Compile error
  3. 20
Show the answer

15. Java is case-sensitive: total and Total are two completely separate variables, 5 and 10.

⚠️ The trap

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;   // ok

Conventions: 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;
🤔 Think first

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 the real world

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

  1. Can't start with a digit, can't contain - or spaces
  2. Case-sensitive: total and Total are different names
  3. Classes PascalCase, methods/variables camelCase
  4. Constants UPPER_SNAKE_CASE, packages lowercase
🤯 Did you know?

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?

  1. $price
  2. _count
  3. 2ndPlace
  4. 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?

  1. They don't compile: constants must be written in capitals
  2. They don't compile: class names must start with a capital letter
  3. javac prints a warning for each badly named identifier
  4. 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.

Next: the words you can never use as names, and sneaky ones like var that you sometimes can.