Java Naming Conventions
Every Indian railway station has a short code: NDLS, CSMT, MAS. Nothing stops a station from being called something else, but because everyone follows the same pattern, you can read a ticket instantly. Java names work the same way. The compiler accepts almost anything, but every Java team follows the same naming conventions, so you can tell at a glance whether a name is a class, a method, a variable or a constant.
The Conventions at a Glance
| Kind of name | Style | Examples |
|---|---|---|
| Class, interface, record, enum | PascalCase: every word starts with a capital | TicketBooking, PaymentService, Runnable |
| Method | camelCase, usually a verb | calculateFare(), printReceipt(), isValid() |
| Variable | camelCase, a noun | passengerCount, farePerPerson, city |
Constant (static final) | UPPER_SNAKE_CASE | MAX_SEATS, GST_RATE, DEFAULT_CITY |
| Package | all lowercase, often a reversed domain | com.poolstring.billing, java.util |
| Enum values | UPPER_SNAKE_CASE | SLEEPER, AC_THREE_TIER |
The Conventions in Code
public class TicketBooking {
static final int MAX_SEATS = 72;
static int calculateFare(int farePerPerson, int passengerCount) {
return farePerPerson * passengerCount;
}
public static void main(String[] args) {
int passengerCount = 4;
int fare = calculateFare(450, passengerCount);
System.out.println("Seats in coach: " + MAX_SEATS);
System.out.println("Fare for " + passengerCount + ": Rs. " + fare);
}
}Seats in coach: 72
Fare for 4: Rs. 1800How it works
- Line 1:
TicketBookingis a class, so it's PascalCase. - Line 2:
MAX_SEATSnever changes, so it's a constant in UPPER_SNAKE_CASE. - Line 4:
calculateFareis a method that does something, so it's a verb in camelCase. Its parameters are camelCase nouns. - Lines 9 to 12 use the names; you can tell each kind apart without looking at its declaration.
Try it yourself: rename calculateFare to CalculateFare everywhere. It still compiles. Why would a teammate still ask you to change it back?
Good Names, Not Just Correct Ones
Following the style is the first step. A name also has to say what the thing is:
| Weak | Better | Why |
|---|---|---|
int n; | int passengerCount; | Says what is counted |
double d; | double discountRate; | Says what the number means |
void calc() | void calculateGst() | Says what is calculated |
boolean flag; | boolean isPaid; | Reads as a yes/no question |
String s1, s2; | String fromCity, toCity; | Tells the two apart |
Short names like i and j are fine for loop counters, where everyone knows what they mean.
Common Mistakes
Using underscores in variable names. passenger_count is Python style. In Java, write passengerCount; underscores are only for constants.
Lowercase class names. class ticket compiles, but every Java developer reads ticket as a variable, which makes code confusing.
Abbreviating everything. calcTktFr() saves a few letters and costs every reader time. Modern IDEs complete long names for you.
Practice
- Easy: Fix the style of these names:
class bank_account,int Balance,void Deposit(),final static double gstrate,package Com.PoolString.Billing. - Harder: Write a class for a library book with a constant for the late fee per day, a method that works out the fine, and variables for the number of late days. Use the conventions from this lesson.
Key Takeaways
Was this tutorial helpful?