Java Naming Conventions

Naveen AnamaneniUpdated · 4 min read

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

One file, every convention
Kind of nameStyleExamples
Class, interface, record, enumPascalCase: every word starts with a capitalTicketBooking, PaymentService, Runnable
MethodcamelCase, usually a verbcalculateFare(), printReceipt(), isValid()
VariablecamelCase, a nounpassengerCount, farePerPerson, city
Constant (static final)UPPER_SNAKE_CASEMAX_SEATS, GST_RATE, DEFAULT_CITY
Packageall lowercase, often a reversed domaincom.poolstring.billing, java.util
Enum valuesUPPER_SNAKE_CASESLEEPER, AC_THREE_TIER

The Conventions in Code

TicketBooking.java▶ Try it
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);
    }
}
Output
Seats in coach: 72
Fare for 4: Rs. 1800

How it works

  1. Line 1: TicketBooking is a class, so it's PascalCase.
  2. Line 2: MAX_SEATS never changes, so it's a constant in UPPER_SNAKE_CASE.
  3. Line 4: calculateFare is a method that does something, so it's a verb in camelCase. Its parameters are camelCase nouns.
  4. 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:

WeakBetterWhy
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

  1. Easy: Fix the style of these names: class bank_account, int Balance, void Deposit(), final static double gstrate, package Com.PoolString.Billing.
  2. 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.

Quick check: Naming conventions

4 questions
1Which name follows the convention for a class?
2How should a constant be named?
3Which is the best name for a boolean that says whether a ticket is confirmed?
4Does breaking a naming convention stop the code from compiling?

Key Takeaways

Was this tutorial helpful?

Lesson 29 of 482