What is the best practice for java constants class?

543    Asked by JackGREEN in Java , Asked on Oct 6, 2022

I create a constant class by looking at some of the best practices described here in stackoverflow. Many of those have a single Constant class with variables. Some of the answers suggested creating separate Contact Classes. The way I create a constant class is by naming it Parameters and creating classes i.e. Variables and Constants. Then these classes further have child classes e.g. URL, Columns etc. This time I created a constant class with the same structure and a separate class named ReportTemplate. This is my first time creating a Constant class of Objects that don't have a primitive data type.


public final class ReportTemplate {
   public final static class ColumnIds {
        public static final String TITLE_COLUMN_ID = "title";
        public static final String TYPE_COLUMN_ID = "type";
        public static final String LIFECYCLESTATUS_COLUMN_ID = "lifecycle status";
        public static final String INSERTIONTIMESTAMP_COLUMN_ID = "insertionTimestamp";
    }
   public final static class Columns {
        public static final TextColumnBuilder TITLE = col.column(
                "Title", ColumnIds.TITLE, type.stringType());
public static final TextColumnBuilder LIFECYCLESTATUS = col.column(
                "Lifecycle Status", ColumnIds.LIFECYCLESTATUS,
                type.stringType());
        public static final TextColumnBuilder TYPE = col.column("Type",
                ColumnIds.TYPE, type.stringType());
        public static final TextColumnBuilder INSERTIONTIMESTAMP = col
                .column("Insertion Timestamp",
                        ColumnIds.INSERTIONTIMESTAMP,
                        type.stringType());
    }
 public final static class Styles {
        public static final StyleBuilder HEADING1 = style.style()
                .setName("heading 1").bold().setFontSize(15);
        public static final StyleBuilder HEADING2 = stl.style().setName("heading2")
                .bold().setFontSize(12);
        public static final StyleBuilder HEADING3 = style.style().setName("heading3")
                .bold().setFontSize(10);
    }
}

I want to know if my

Naming Scheme is correct Constant class structure is among best practices.

Should I create separate constant classes or have them encapsulated in a parent Constant


Answered by Jack Russell

Is the Naming Scheme correct? -


Ideally you should not create a class with names like Variables, Parameters, etc. as these names also have literal meanings with them in many languages. Besides that, these classes are going to store Constants! You can simply create a single class with the name Constants! No need to create a different Java Constant class till it's absolutely necessary. Perhaps, in that case you should go for enums. Why? Check this link:

https://gorbeia.wordpress.com/2015/03/11/java-enums-vs-constants/

Constant class structure is among best practices ?

This is answered in the above point. Keep your naming convention readable enough so that you won't require separate constant classes. Always try to implement KISS(Keep it Simple Stupid) and DRY(Don't repeat yourself!)

Should I create separate constant classes or have them encapsulated in a parent Constant class is fine?

Ideally not. If you need such a requirement then go for enums. As enums are typesafe, you wont end up creating misleading statements. For example, If you write following potentially incorrect code then Constant class won't mind:

String playerType = Constants.MALE;

But, if you use enums, that would end up as:

// Compile-time error - incompatible types!

PlayerType playerType = Gender.MALE;



Your Answer

Interviews

Parent Categories