HiveBrain v1.2.0
Get Started
← Back to all entries
principlejavaMinor

Best practice for Constant Class in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
constantpracticejavaforclassbest

Problem

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 Contant 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 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 datatype.

```
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 = "lifecyclestatus";
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 = stl.style()
.setName("heading1").bold().setFontSize(15);

public static final StyleBuilder HEADING2 = stl.style().setName("heading2")
.bold().setFontSize(12);

public static final Sty

Solution

Answer to your questions :

Naming Scheme is correct? -

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

-
Link 1.

-
Link 2

Constant class structure is among best practices ?

This is answered in 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 having them encapsulated in a parent Constant class is fine?

Ideally not. If you need such requirement then go for enums. As enums are typesafe, you wont end up creating misleading statements. For ex.
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;

Code Snippets

String playerType = Constants.MALE;
// Compile-time error - incompatible types!
PlayerType playerType = Gender.MALE;

Context

StackExchange Code Review Q#125415, answer score: 5

Revisions (0)

No revisions yet.