Master Java Naming Conventions: From Packages to Enums
This comprehensive guide explains Java naming standards for packages, classes, interfaces, abstract classes, exceptions, methods, variables, constants, enums, generics, and test code, providing clear rules, examples, and best‑practice code snippets to help developers write clean, maintainable code.
1. Package Naming Convention
Packages group related classes and interfaces, using lowercase letters and dot separators; each segment should be a single noun. Typically, a top‑level domain (e.g., com, org, net) is used as a prefix, followed by organization and module names.
package org.springframework.boot.autoconfigure.cloud
package org.springframework.boot.util
package org.hibernate.action
package org.hibernate.cfg
package com.alibaba.druid
package com.alibaba.druid.filter
package com.alibaba.nacos.client.config
package com.ramostear.blog.webCommon Oracle Java packages:
package java.beans
package java.io
package java.lang
package java.net
package java.util
package javax.annotation2. Class Naming Convention
Class names use nouns with an initial capital letter; multi‑word names follow CamelCase, capitalizing each word. Avoid abbreviations unless widely accepted (e.g., i18n, URI, DAO, JWT, HTML).
public class UserDTO {}
class EmployeeService {}
class StudentDAO {}
class OrderItemEntity {}
public class UserServiceImpl {}
public class OrderItemController {}2.1 Interface Naming Convention
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 Comparable<T> {}
public interface CompletionService<V> {}
public interface Iterable<T> {}
public interface EventListener {}2.2 Abstract Class Naming Convention
Abstract classes follow normal class rules; to distinguish them, prepend "Abstract" or "Base".
public abstract class AbstractRepository<T> {}
public abstract class AbstractController {}
public abstract class BaseDao<T,ID> {}
public abstract class AbstractCommonService<T> {}2.3 Exception Class Naming Convention
Exception classes 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 IndexOutOfBoundsException {}
public abstract 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, and to.
3.1 Getter
public String getUserName() {}
public List<Integer> getUserIds() {}
public User getOne() {}3.2 Finder/Query
public List<User> findOne(Integer id) {}
public List<Integer> findAll() {}
public List<String> queryOrders() {}3.3 Conditional
public User findByUsername(String username) {}
public List<Integer> getUserIdsWithState(boolean state) {}
public List<User> findAllByUsernameOrderByIdDesc(String username) {}3.4 Setter/Action
public void setName(String name) {}
public User insert(User user) {}
public void update(User user) {}
public void clearAll() {}3.5 Others
Length/size methods use "length" or "size"; boolean methods use "is" or "has"; conversion methods use "to".
public long length() {}
public int size() {}
public boolean isOpen() {}
public boolean isNotEmpty() {}
public boolean hasLength() {}
public Set<Integer> mapToSet(Map map) {}
public UserDto convertTo(User user) {}
public String toString(Object obj) {}4. Variable Naming Convention
Variables start with a lowercase letter; multi‑word names capitalize each subsequent word. 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 use all uppercase letters 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 = 1000;
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
7.1 Arrays
int[] array = new int[10];
int[] idArray = {1,2,3,4,5};
String[] nameArray = {"First","Yellow","Big"};
public List<String> getNameById(Integer[] ids) {}
public List<String> getNameById(Integer... ids) {}7.2 Collections
Collection<Order> orders;
int[] values;
List<Item> items;
Map<String,User> userMap;
Map<String,List<Object>> listMap;7.3 Generics
Common generic placeholders: E (Element), ID (Identifier), T (Type), K (Key), V (Value), N (Number), X (Exception), U/S (any type).
public class HashSet<E> extends AbstractSet<E> {}
public class HashMap<K,V> extends AbstractMap<K,V> {}
public class ThreadLocal<T> {}
public interface Functor<T,X extends Throwable> { T val() throws X; }
public class Container<K,V> { private K key; private V value; }
public interface BaseRepository<T,ID> { T findById(ID id); void update(T t); List<T> findByIds(ID...ids); }
public static <T> List<T> methodName(Class<T> clz) { List<T> dataList = getByClz(clz); return dataList; }7.4 Interface Implementation
Implementation classes should use the "Impl" suffix; avoid prefixing interfaces with "I".
public interface OrderService {}
public class OrderServiceImpl implements OrderService {}7.5 Test Classes and Methods
public class UserServiceTest {
public void testFindByUsernameAndPassword() {}
public void testUsernameExist_notExist() {}
public void testDeleteById_isOk() {}
}8. Object Types Overview
Name
Scope
Description
BO
Service/Manager/Business classes
Business Object – encapsulates business logic.
DTO
Processed PO objects
Data Transfer Object – used for remote data transfer.
DAO
Database access classes
Data Access Object – encapsulates DB operations.
PO
Bean/Entity classes
Persistant Object – represents a row in a database table.
POJO
General Java objects
Plain Ordinary Java Object – simple Java object without special naming.
VO
View layer data objects
Value Object – used between controller and view.
AO
Application layer objects
Application Object – rarely used abstraction between web and service layers.
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.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
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.
