Naming Conventions in Java
1. Why Names Matter
Good names = readable code = fewer bugs + marks for style. Java community follows camelCase + PascalCase rules (not enforced by compiler, but checked by teachers/reviewers).
2. Rules Table (memorize)
| Element | Rule | Good | Bad |
|---|---|---|---|
| Class | PascalCase (noun) | Student, BankAccount |
student, bank_account |
| Method | camelCase (verb) | getMarks(), printBill() |
GetMarks(), print_bill() |
| Variable | camelCase, short but clear | totalMarks, i (loop ok) |
TotalMarks, x1x |
| Constant | UPPER_SNAKE | MAX_MARKS, PI |
maxMarks |
| Package | lowercase, reversed domain | com.college.fees |
Com.College |
3. Code — Good vs Bad
// BAD (works but ugly, loses style marks)
class student { int TOTAL; void Print() { TOTAL = 100; } }
// GOOD (clean, professional)
class Student {
static final int MAX_MARKS = 100;
int totalMarks;
void printTotal() { System.out.println(totalMarks); }
}
4. Beginner Rules That Break Code
- Must start with letter/
_/$(not digit):marks1ok,1markserror. - Case-sensitive:
Marks≠marks. - No keywords:
class,int,newcannot be names.
5. Exam Points
- Table of 4-5 conventions with examples — 5 marks, very frequent.
- Constant + package rules — 2-3 marks, frequent.