Multiple constants in same class

I want to know what is the best way to use final constants in the same class? since i have to use a lot for the same class and looks very meshy, thanks.

How i can make it look more readable, since if I need to store more variables there will be many……….. thanks

public class Default {

private static final String CLASS_FILE_EXTENSION = ".class";

private static final String JAVA_PACKAGE = "java.";

private static final String NO_PREFIX = "";

private static final String SUFFIX = "sfx";

private static final String TEMP_SUFFIX = "tmp";

private static final String NO_PARAMETERS = "()";

private static final String STRING_DESCRIPTOR = "Ljava/lang/String;";

private static final String RETURNS_VOID = "V";

private static final String JAR = "jar";

private static final int ONLY = 0;

private static final int BUFFER_SIZE = 1024;

private static final int PACKAGE_LOOKUP = 0x8;

private static final int BASE_VERSION = 44;

private static final Class<?> NOT_FOUND = null;

private static final URL NOT_SEALED = null;

private final TypeDescription instrumentedType;

private final TypeAttributeAppender typeAttributeAppender;

private final AsmVisitorWrapper asmVisitorWrapper;

private final ClassFileVersion classFileVersion;

private final AuxiliaryType.NamingStrategy auxiliaryTypeNamingStrategy;

private final AnnotationValueFilter.Factory annotationValueFilterFactory;

private final AnnotationRetention annotationRetention;

private final Implementation.Context.Factory implementationContextFactory;

private final MethodGraph.Compiler methodGraphCompiler;

private final TypeValidation typeValidation;

private final ClassWriterStrategy classWriterStrategy;

private final LatentMatcher<? super MethodDescription> ignoredMethods;

private final List<DynamicType> auxiliaryTypes;

private final ClassFileLocator classFileLocator;

public Default(..... etc)  

Answer

There is no objective answer possible for this. A few points to consider:

  1. Having a lot of constants in one class might mean that the class is doing a lot of work. See if you can split the class into multiple classes and declare the constants as per the usage within those classes.

  2. Since all these are private fields, see if they are really being used more than once in the same class. If not you can directly use the string rather than having a constant for that. The downside is, in the future, if you want to use it again in the same class at a different place, you have to remember to make it a constant (which is very easy to forget).

  3. (I don’t prefer this) I have seen cases where people will have an interface just for holding constants and static import them into the class to use it. That interface is like a big dumping ground for the constants.

Leave a Reply

Your email address will not be published. Required fields are marked *