Java Coding Templates Based on
Defined Coding Standards
1. Overview
This document provides standard coding templates for Java development based on best
practices and defined coding conventions. These templates aim to improve code readability,
consistency, and maintainability across the project.
2. Naming Conventions
Element Convention Example
Class PascalCase CustomerService
Interface PascalCase (adjective) Serializable
Method camelCase calculateTotal()
Variable camelCase orderAmount
Constant UPPER_CASE_WITH_UNDER MAX_RETRY_COUNT
SCORES
Package lowercase.with.dots com.company.project
3. Class Template
package com.company.project.module;
import java.util.List;
/**
* Class description.
*
* @author
*/
public class ClassName {
// Constants
private static final int DEFAULT_TIMEOUT = 30;
// Member Variables
private String name;
private int count;
// Constructor
public ClassName(String name, int count) {
this.name = name;
this.count = count;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// Business Methods
public void process() {
// TODO: Implement logic
}
// Overridden Methods
@Override
public String toString() {
return "ClassName{name=" + name + ", count=" + count + "}";
}
}
4. Interface Template
package com.company.project.service;
/**
* Interface for XYZ functionality.
*/
public interface ServiceInterface {
void performAction();
String getStatus();
}
5. Enum Template
package com.company.project.enums;
/**
* Enum representing types of orders.
*/
public enum OrderType {
ONLINE,
IN_STORE,
PICKUP;
}
6. Exception Template
package com.company.project.exception;
/**
* Custom exception for specific error scenarios.
*/
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
public CustomException(String message, Throwable cause) {
super(message, cause);
}
}
7. Unit Test Template (JUnit 5)
package com.company.project.module;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ClassNameTest {
private ClassName className;
@BeforeEach
void setUp() {
className = new ClassName("Test", 10);
}
@Test
void testGetName() {
assertEquals("Test", className.getName());
}
}
8. Logging Standards
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Example {
private static final Logger logger = LoggerFactory.getLogger(Example.class);
public void run() {
logger.info("Processing started.");
try {
// Business logic
} catch (Exception e) {
logger.error("Error occurred while processing", e);
}
}
}
9. JavaDoc Standards
/**
* Calculates the total price including tax.
*
* @param basePrice The base price of the item.
* @param taxRate The tax rate as a decimal (e.g., 0.05 for 5%).
* @return The total price.
*/
public double calculateTotal(double basePrice, double taxRate) {
return basePrice + (basePrice * taxRate);
}
10. Additional Notes
- Avoid magic numbers; use named constants.
- Keep methods short and focused.
- Use meaningful variable and method names.
- Follow SOLID principles and separation of concerns.