Classes
Following the standard Java convention.
package com.gpcoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* Class documentation comment...
*/
public class ClassOrganization {
/**
* Class ( static) variables
*/
public static String PUBLIC_STATIC_VAR = "";
protected static String PROTECTED_STATIC_VAR = "";
static String DEFAULT_STATIC_VAR = "";
private static String PRIVATE_STATIC_VAR = "";
/**
* Instance variables
*/
public String publicStaticVar = "";
protected String protectedStaticVar = "";
String defaultStaticVar = "";
private String privateStaticVar = "";
/**
* Constructors
*/
public ClassOrganization() {
}
/**
* Methods
* <p>
* These methods should be grouped by functionality rather than by scope or accessibility.
* For example, a private class method can be in between two public instance methods.
* The goal is to make reading and understanding the code easier.
*/
public void doSomething1() {
task1();
task2();
}
private void task1() {
}
private void task2() {
}
public void doSomething2() {
task3();
}
private void task3() {
}
}
We like to keep our variables and utility functions private, but we’re not fanatic about it. Sometimes we need to make a variable or utility function protected so that it can be accessed by a test.
The first rule of classes is that they should be small. The second rule of classes is that they should be smaller than that.
The name of a class should describe what responsibilities it fulfills. In fact, naming is probably the first way of helping determine class size. If we cannot derive a concise name for a class, then it’s likely too large. The more ambiguous the class name, the more likely it has too many responsibilities. For example, class names including weasel words like
Processor
or Manager
or Super
often hint at unfortunate aggregation of responsibilities.public final class CommonUtils {
private CommonUtils() {
}
public static String toJson() {
}
public static <T> T fromJson(String json, Class<T> tClass) {
}
public static String toJson(Object obj) {
}
public static String concat(String delimiter, String... str) {
}
public static boolean isNotBlank(String str) {
}
public static <T> List<T> subList(List<T> list, int start, int numberOfElement) {
}
public static String randomString(int length) {
}
}
public final class JsonUtils {
private JsonUtils() {
}
public static String toJson() {
}
public static <T> T fromJson(String json, Class<T> tClass) {
}
public static String toJson(Object obj) {
}
}
public final class StringUtils {
private StringUtils() {
}
public static String concat(String delimiter, String... str) {
}
public static boolean isNotBlank(String str) {
}
}
public final class CollectionUtils {
private CommonUtils() {
}