Naming conventions
Use the names the whole ecosystem expects, so your code reads like the standard library.
Open this lesson in the learning hubKey points
- Classes, records, interfaces and enums are
UpperCamelCasenouns:CustomerOrder. - Methods and variables are
lowerCamelCase, and a method name starts with a verb:findById. static finalconstants areUPPER_SNAKE_CASE:MAX_LOGIN_ATTEMPTS.- Packages are all lower case with no underscores, usually a reversed domain:
com.acme.orders. - Boolean methods read as a question:
isEmpty(),hasNext(). Avoiddataandtemp. - None of this is enforced by the compiler, but every tool, reviewer and library assumes it.
Example
public class Main {
static final int MAX_LOGIN_ATTEMPTS = 3;
enum OrderStatus { NEW, SHIPPED, CANCELLED }
record CustomerOrder(String customerName, int itemCount) {}
static boolean isShippable(CustomerOrder order) {
return order.itemCount() > 0;
}
public static void main(String[] args) {
CustomerOrder pendingOrder = new CustomerOrder("Ada", 3);
System.out.println("class / record -> CustomerOrder : " + pendingOrder);
System.out.println("variable -> pendingOrder : " + pendingOrder.customerName());
System.out.println("method verb -> isShippable() : " + isShippable(pendingOrder));
System.out.println("constant -> MAX_LOGIN_ATTEMPTS = " + MAX_LOGIN_ATTEMPTS);
System.out.println("enum constant -> " + OrderStatus.SHIPPED);
System.out.println("package -> com.acme.orders, all lower case");
System.out.println("names are the cheapest documentation you will ever write");
}
}
Follow the conventions and your code stops needing an explanation.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Core Java course, and every lesson in it is listed on the Core Java contents page.