Java Naming Conventions
Standard naming rules for Java classes, methods, variables, constants, and packages used across the ecosystem.
The Java compiler doesn't care what you name things, as long as the identifier rules are followed. The community, however, follows a tight set of naming conventions that have been the same since the language was released. Sticking to them makes your code look native to anyone reading it, and many IDE inspections and lint rules assume them.
The conventions at a glance
| Construct | Convention | Example |
|---|---|---|
| Class, interface, enum, record | UpperCamelCase | BankAccount, Order, Color |
| Method, variable, parameter, field | lowerCamelCase | transferFunds, lineCount |
Constant (static final) | UPPER_SNAKE_CASE | MAX_RETRIES, DEFAULT_TIMEOUT |
| Package | all lowercase, dot-separated | com.example.billing |
| Type parameter | single uppercase letter | T, E, K, V |
Examples in code:
package com.example.billing;
public class InvoicePrinter {
public static final int MAX_LINE_WIDTH = 80;
private int lineCount;
public void print(Invoice invoice) {
for (LineItem item : invoice.getItems()) {
renderLine(item);
}
}
private void renderLine(LineItem item) { ... }
}Classes, interfaces, enums, records
Use UpperCamelCase — capitalize the first letter of every word, no separators:
Customer,OrderRepository,HttpClient,XmlParser(initialisms are usually treated as words:Http,Xml)
Class names should be nouns: Order, Connection, BankAccount.
Interfaces are also UpperCamelCase. Two common naming patterns:
- Adjective ending in
-able:Comparable,Runnable,Serializable. - Noun naming a role:
List,Repository,Connection.
Avoid the old Hungarian-notation I prefix (ICustomer) — Java code doesn't do that.
Methods and variables
Use lowerCamelCase: first word lowercase, every subsequent word capitalized:
calculateTotal,parseDate,getUserName,index,lineCount.
Method names should be verbs or verb phrases:
save,findById,validate,parseJson.
Common verb prefixes:
get/set— accessor and mutator (also called getter and setter).is/has/can— return a boolean:isEmpty,hasNext,canExecute.to— return a converted form:toString,toUpperCase.from— factory method:LocalDate.from(temporal).
Variable names should describe what the value is, not how it's used. Prefer customer over obj, lineCount over n. Single-letter names are OK for loop indices (i, j) and short-lived locals where the type is obvious (var p = new Point(...)).
Constants
A constant is a static final field. Use UPPER_SNAKE_CASE:
public static final int MAX_RETRIES = 3;
public static final String DEFAULT_GREETING = "Hello";
public static final Duration TIMEOUT = Duration.ofSeconds(30);Local final variables (one-shot bindings inside a method) are NOT considered constants in the same sense — keep them lowerCamelCase:
public void process(Order o) {
final int maxAttempts = 3; // not MAX_ATTEMPTS
...
}Packages
Package names are all lowercase, separated by dots. The community convention is to use a reversed domain you control:
com.google.guavaorg.apache.commons.lang3com.example.billing.invoices
Avoid underscores or capital letters in package names — they're considered non-idiomatic.
Type parameters
Generic type parameters are usually single uppercase letters. The de-facto conventions:
T— type (general)E— element (in a collection)K— keyV— valueR— return typeS,U— second, third type parameter when more than one
public interface List<E> { ... }
public interface Map<K, V> { ... }
public interface Function<T, R> { ... }For complex APIs where a single letter is unclear, use a descriptive name in UpperCamelCase ending in T: RequestT, ResponseT. This is rare.
Booleans
Boolean variable and method names are usually phrased as predicates:
isActive,hasNext,canExecute,shouldRetry.
Avoid negative names like isNotEmpty — they read poorly when combined with !.
Naming smells to avoid
- One-letter names outside a tight loop:
int s = 100reads as nothing. - Hungarian notation:
strName,iCount— Java is statically typed, the IDE shows the type. - Numeric suffixes:
total1,total2,processData2. If you need two, find a real distinction. - All-uppercase enum-style constants for everything
final: only true module-level constants get the UPPER_SNAKE treatment. - Inconsistent casing across siblings: if one method is
getUserId, the neighbour shouldn't beget_email.
A demonstration
Every name here follows the standard convention: class in UpperCamelCase, constant in UPPER_SNAKE_CASE, variables in lowerCamelCase, boolean named as a predicate.
What's next
Java Data Types introduces Java's primitive and reference types — the building blocks every variable is made of.
Practice
Which name follows the standard Java convention for a constant?