gmazzo/gradle-buildconfig-plugin

Why the ctor?

Desoroxxx opened this issue · 4 comments

I am using the plugin with Groovy and Java, and the generated class has a private ctor, my question is why not remove the ctor?

Abuse of language sorry it should be clearer what I meant

public final class BuildConfig {
public static final String APP_NAME = "example-generic";
public static final String APP_SECRET = "Z3JhZGxlLWphdmEtYnVpbGRjb25maWctcGx1Z2lu";
public static final long BUILD_TIME = 172800000L;
public static final boolean FEATURE_ENABLED = true;
private BuildConfig() {
}
}

This is standard practice in the Java world. If a class only has static fields, then the class is meaningless when instantiated. These are called "utility classes". Checkstyle even has a rule to enforce hidden ctor: https://checkstyle.sourceforge.io/checks/design/hideutilityclassconstructor.html

Another real world example from a well-known library:
https://github.com/apache/commons-collections/blob/ac05d8fdbe62bbfe051e3b502e5f0fcfbba56c9b/src/main/java/org/apache/commons/collections4/ComparatorUtils.java#L47-L50

Note: if one deletes a private constructor, Java automagically generates a public zero-args constructor, see first sentence at Java specification: https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8.9

Oh that makes sense I never thought about it this way, thanks