Java Naming Conventions: Packages, Classes, Interfaces, Methods, Variables, Constants, Enums and More
This article provides a comprehensive guide to Java naming conventions, covering package naming, class and interface naming, abstract and exception classes, method and variable naming, constant and enum standards, as well as best‑practice examples and code snippets for each rule.
In this article, the author, a senior architect, systematically presents Java naming conventions from the outermost package level down to variables and constants, offering practical examples for everyday development.
1. Package Naming Convention
Packages should be named using all‑lowercase letters, separated by dots, typically starting with a top‑level domain (e.g., com, org) followed by the organization and module name.
package org.springframework.boot.autoconfigure.cloud
package org.springframework.boot.util
package org.hibernate.action
package com.alibaba.druid
package com.alibaba.druid.filter
package com.alibaba.nacos.client.config
package com.ramostear.blog.webCommon Oracle Java packages include java.beans, java.io, java.lang, java.net, java.util, and javax.annotation.
2. Class Naming Convention
Class names use nouns with the first letter capitalized; multi‑word names follow CamelCase. Avoid unnecessary abbreviations except well‑known ones (e.g., i18n, DAO, JWT).
public class UserDTO {}
public class EmployeeService {}
public class StudentDAO {}
public class OrderItemEntity {}
public class UserServiceImpl {}
public class OrderItemController {}2.1 Interface Naming
Interfaces are named like classes but often use adjectives or verbs to describe behavior.
public interface Closeable {}
public interface Cloneable {}
public interface Runnable {}
public interface CompletionService<V> {}
public interface Iterable<T> {}2.2 Abstract Class Naming
Abstract classes usually start with Abstract or Base to distinguish them.
public abstract class AbstractRepository<T> {}
public abstract class AbstractController {}
public abstract class BaseDao<T, ID> {}
public abstract class AbstractCommonService<T> {}
public abstract class AbstractAspectJAdvice {}
public abstract class AbstractSingletonProxyFactoryBean {}
public abstract class AbstractCachingConfiguration {}
public abstract class AbstractContextLoaderInitializer {}2.3 Exception Class Naming
Exception classes must end with Exception. System errors end with Error.
public class FileNotFoundException {}
public class UserAlreadyExistException {}
public class TransactionException {}
public class ClassNotFoundException {}
public class IllegalArgumentException {}
public class VirtualMachineError {}
public class StackOverflowError {}
public class OutOfMemoryError {}
public class IllegalAccessError {}
public class NoClassDefFoundError {}
public class NoSuchFieldError {}
public class NoSuchMethodError {}3. Method Naming Convention
Method names start with a lowercase letter; multi‑word names use camelCase. Common prefixes include get, find, query, set, insert, update, delete, is, has, to, etc.
public String getUserName() {}
public List<Integer> getUserIds() {}
public User getOne() {}
public User findByUsername(String username) {}
public List<Integer> getUserIdsWithState(boolean state) {}
public List<String> getNameById(Integer[] ids) {}
public void setName(String name) {}
public User insert(User user) {}
public void update(User user) {}
public long length() {}
public int size() {}
public boolean isOpen() {}
public boolean hasLength() {}
public Set<Integer> mapToSet(Map map) {}
public String toString(Object obj) {}
public void testFindByUsernameAndPassword() {}
public void testUsernameExist_notExist() {}
public void testDeleteById_isOk() {}4. Variable Naming Convention
Variables start with a lowercase letter; multi‑word names use camelCase. Avoid leading underscores.
private String nickName;
private String mobileNumber;
private Long id;
private String username;
private Long orderId;
private Long orderItemId;5. Constant Naming Convention
Constants are written in uppercase with underscores separating words.
public static final String LOGIN_USER_SESSION_KEY = "current_login_user";
public static final int MAX_AGE_VALUE = 120;
public static final int DEFAULT_PAGE_NO = 1;
public static final long MAX_PAGE_SIZE = 1000L;
public static final boolean HAS_LICENSE = false;
public static final boolean IS_CHECKED = false;6. Enum Naming Convention
Enum types follow class naming rules; enum values follow constant naming rules.
public enum Color { RED, YELLOW, BLUE, GREEN, WHITE }
public enum PhysicalSize { TINY, SMALL, MEDIUM, LARGE, HUGE, GIGANTIC }
public enum ElementType { TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE }7. Other Naming Conventions
Arrays, collections, generic types, and test classes also have specific guidelines, such as using plural nouns for collections, suffix Impl for implementation classes, and naming test methods with test prefix.
int[] array = new int[10];
int[] idArray = {1,2,3,4,5};
String[] nameArray = {"First", "Yellow", "Big"};
public interface OrderService {}
public class OrderServiceImpl implements OrderService {}
public class UserServiceTest {
public void testFindByUsernameAndPassword() {}
public void testUsernameExist_notExist() {}
public void testDeleteById_isOk() {}
}8. Object Types Overview (BO, DTO, DAO, PO, POJO, VO, AO)
Name
Scope
Description
BO
Service/Manager/Business layer
Business Object – encapsulates business logic.
DTO
Data transfer between layers or services
Data Transfer Object – may contain a subset of PO fields.
DAO
Data access layer
Data Access Object – handles database CRUD.
PO
Entity/Bean layer
Persistant Object – represents a table row.
POJO
General Java objects
Plain Ordinary Java Object – generic term for simple objects.
VO
View layer
Value Object – used to transfer data to the UI.
AO
Application layer
Application Object – rarely used, sits between Web and Service.
Images illustrating the relationships between these object types are included in the original article.
Additional promotional sections and QR‑code images are present but omitted from the technical summary.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
