Java Naming Standards: Difference between revisions
Jump to navigation
Jump to search
Created page with "=Introduction= I guess I run with whatever the linter tells me but wanted to capture '''a way''' here =Naming Standards= Here we go. {| class="wikitable" ! Type !! Convention !! Example |- | Variables || camelCase || <code>String userName = "John";</code> |- | Methods || camelCase || <code>public void calculateTotal() { ... }</code> |- | Parameters || camelCase || <code>public User getUser(String userId) { ... }</code> |- | Classes || PascalCase || <code>public class Us..." |
|||
Line 35: | Line 35: | ||
|- | |- | ||
| JUnit test methods || camelCase with descriptive names || <code>void shouldThrowExceptionWhenInvalidInput() { ... }</code> | | JUnit test methods || camelCase with descriptive names || <code>void shouldThrowExceptionWhenInvalidInput() { ... }</code> | ||
|} | |||
And... | |||
{| class="wikitable" | |||
! Convention !! Description !! Example | |||
|- | |||
| Acronyms in Names || Two-letter acronyms are uppercase; longer acronyms capitalize only the first letter. || <code>IOStream</code>, <code>HttpRequest</code> | |||
|- | |||
| Bean Properties || Use <code>getPropertyName()</code> or <code>isPropertyName()</code> for getters, and <code>setPropertyName()</code> for setters. || <code>public String getUserName()</code>, <code>public void setUserName(String userName)</code> | |||
|- | |||
| Abstract Classes || Prefix abstract classes with "Abstract". || <code>public abstract class AbstractRepository</code> | |||
|- | |||
| Implementation Classes || Suffix implementation classes with "Impl". || <code>public class UserServiceImpl</code> | |||
|- | |||
| Inner Classes || Use PascalCase like regular classes. || <code>public class UserAccountHelper</code> | |||
|- | |||
| Annotations || Use PascalCase for custom annotation types. || <code>public @interface CustomAnnotation</code> | |||
|- | |||
| Lambda Parameters || Use single-letter names for simple lambdas; use descriptive names for complex lambdas. || <code>x -> x * 2</code>, <code>(user, role) -> user.hasRole(role)</code> | |||
|- | |||
| Maven/Gradle Identifiers || Use lowercase for group IDs and hyphenated lowercase for artifact IDs. || <code>com.example</code>, <code>user-service</code> | |||
|} | |} |
Latest revision as of 00:28, 4 July 2025
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
|