Java Naming Best Practices
Practical naming rules for Java packages, classes, methods, variables, and constants.
Java Naming Best Practices
Names are the cheapest documentation you will ever write and the most expensive to get wrong. Java has a strong, near-universal set of naming conventions — codified in the original Java Language Specification and reinforced by the JDK, every major library, and tools like Checkstyle. Following them is not about taste: it lets any Java reader infer what a thing is from how it is written, before reading a single line of its body. This chapter collects the rules that matter and shows them at work.
The case conventions at a glance
Java assigns a distinct casing style to each kind of identifier. The casing alone tells the reader the category:
| Identifier | Convention | Example |
|---|---|---|
| Package | all lowercase, dotted, reverse-domain | com.w3docs.billing |
| Class / interface / enum / record | PascalCase (UpperCamelCase) noun | OrderLine, HttpClient |
| Method | camelCase verb phrase | calculateGrossTotal, isBulk |
| Variable / parameter / field | camelCase noun | taxRate, orderLines |
Constant (static final) | UPPER_SNAKE_CASE | MAX_RETRY_COUNT |
| Type parameter | single uppercase letter | E, T, K, V |
package com.w3docs.billing; // lowercase, reverse-domain
public class InvoiceService { // PascalCase class
private static final int MAX_RETRY_COUNT = 3; // UPPER_SNAKE_CASE constant
private final TaxTable taxTable; // camelCase field
public BigDecimal calculateTotal(List<LineItem> lineItems) { ... } // camelCase method
}Packages: lowercase and reverse-domain
Package names are always lowercase, with no underscores, and start from a domain you control reversed — com.w3docs.billing, not Billing or w3docs_billing. The reverse-domain prefix keeps your packages globally unique so they never collide with a third-party library on the classpath. Avoid Java keywords and digits as the first character of any segment.
package com.w3docs.billing.tax; // good: lowercase, reverse-domain, dotted
// package Com.W3docs.Billing; // bad: uppercase segments
// package com.w3docs.2024billing;// bad: segment starts with a digitClasses are nouns, methods are verbs
A class, interface, enum, or record models a thing, so its name is a noun phrase in PascalCase: Order, PaymentGateway, OrderLine. A method does something, so its name is a verb phrase in camelCase: calculateTotal, sendInvoice, parseDate. Two strong sub-conventions follow:
- Methods that return a
booleanread as a question:isEmpty,hasNext,canRetry. - Classic accessors use the
get/setprefix (getName,setName) — but Java records generate accessors named after the field with no prefix (name(), notgetName()).
interface PaymentGateway { // noun, PascalCase
boolean isAvailable(); // boolean → question form
Receipt charge(Money amount); // verb phrase
}
record Customer(String name, String email) { } // accessors: name(), email()Constants, variables, and type parameters
A static final constant whose value is fixed at compile time uses UPPER_SNAKE_CASE so it stands out from ordinary variables: MAX_RETRY_COUNT, DEFAULT_TAX_RATE. Local variables, parameters, and fields are camelCase nouns that describe the value, not its type — prefer taxRate over d or theDouble. Generic type parameters are single uppercase letters by long-standing convention:
| Letter | Conventional meaning |
|---|---|
E | Element (in a collection) |
T | Type (a general type) |
K, V | Key and Value (in a map) |
R | Return type |
N | Number |
static final double DEFAULT_TAX_RATE = 0.20; // constant
public <K, V> Map<K, V> copyOf(Map<K, V> source) { ... } // type params K, VAvoid these common mistakes
A few anti-patterns show up again and again. They compile, but they fight the reader:
- Hungarian / type-encoded names —
strName,iCount,lstOrders. The type is already in the declaration; the name should describe meaning. - Single-letter locals beyond loop counters —
c,x,tmpfor a domain value hide intent. - Cryptic abbreviations —
calcGrsTtl. Spell words out; modern IDEs autocomplete. l(lowercase L) andOas variable names — they are visually indistinguishable from1and0.- Class names that are verbs (
ProcessData) or method names that are nouns — preferorder.calculateTotal()for something that computes, reserving the bare noun form for pure accessors.
A worked example: the conventions in one program
This program puts every rule above into a single file — a constant, a record with no-prefix accessors, a boolean query method, a camelCase method with descriptive parameters, and a generic list. Read the source and the output together to see how the casing maps to the category.
What to take from the run:
- The two
UPPER_SNAKE_CASEconstants print asmax retry count : 3anddefault tax rate: 0.2— their casing signals at the use site that these are fixed, shared values, not locals you can reassign. OrderLineis a PascalCase record, and the loop reads its data throughproductName(),quantity(), andlineTotal()— note there is nogetprefix, because record accessors are named exactly after the component.isBulk()printsbulk=truefor the 12-unit line andbulk=falsefor the 1-unit line: theisprefix told you it returned a boolean before you read its body, and the output confirms the convention pays off at the call site.calculateGrossTotalis a verb-phrase method whose parametersorderLinesandtaxRatedescribe meaning, not type — the resultgross total : 111.60(93.00 net times 1.20) is exactly what the method name promised.- The final line uses
List<String>, a generic parameterized with a concrete type, echoing the type-parameter convention (Efor element) the JDK collections are declared with — the program prints[alpha, beta]to show the typed list in action.
Practice
Following standard Java naming conventions, how should a public method that returns whether a cart is empty, and a compile-time constant for the maximum number of items, be named?