Java_Coding_Templates
Java_Coding_Templates
2. Naming Conventions
Element Convention Example
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;
}
// 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 {
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class ClassNameTest {
@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;
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);
}