Java Naming Standards
Jump to navigation
Jump to search
Introduction
I guess I run with whatever the linter tells me but wanted to capture a way here
Naming Standards
Here we go.
Type | Convention | Example |
---|---|---|
Variables | camelCase | String userName = "John";
|
Methods | camelCase | public void calculateTotal() { ... }
|
Parameters | camelCase | public User getUser(String userId) { ... }
|
Classes | PascalCase | public class UserAccount { ... }
|
Interfaces | PascalCase | public interface UserProfile { ... }
|
Enums | PascalCase | public enum UserRole { ADMIN, EDITOR, VIEWER }
|
Enum constants | UPPER_SNAKE_CASE | public enum UserRole { ADMIN, EDITOR, VIEWER }
|
Constants | UPPER_SNAKE_CASE | public static final int MAX_RETRY_ATTEMPTS = 3;
|
Package names | lowercase | package com.example.project;
|
Instance fields | camelCase | private int accountBalance;
|
Static fields | camelCase (non-final) | private static int userCount;
|
Generic type parameters | Single uppercase letter | public <T> void process(T item) { ... }
|
File names | PascalCase.java (match class name) | UserAccount.java
|
Test class names | PascalCase with Test suffix | UserAccountTest.java
|
JUnit test methods | camelCase with descriptive names | void shouldThrowExceptionWhenInvalidInput() { ... }
|
And...
Convention | Description | Example |
---|---|---|
Acronyms in Names | Two-letter acronyms are uppercase; longer acronyms capitalize only the first letter. | IOStream , HttpRequest
|
Bean Properties | Use getPropertyName() or isPropertyName() for getters, and setPropertyName() for setters. |
public String getUserName() , public void setUserName(String userName)
|
Abstract Classes | Prefix abstract classes with "Abstract". | public abstract class AbstractRepository
|
Implementation Classes | Suffix implementation classes with "Impl". | public class UserServiceImpl
|
Inner Classes | Use PascalCase like regular classes. | public class UserAccountHelper
|
Annotations | Use PascalCase for custom annotation types. | public @interface CustomAnnotation
|
Lambda Parameters | Use single-letter names for simple lambdas; use descriptive names for complex lambdas. | x -> x * 2 , (user, role) -> user.hasRole(role)
|
Maven/Gradle Identifiers | Use lowercase for group IDs and hyphenated lowercase for artifact IDs. | com.example , user-service
|