4
\$\begingroup\$

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<String> TITLE = col.column(
                "Title", ColumnIds.TITLE, type.stringType());

        public static final TextColumnBuilder<String> LIFECYCLESTATUS = col.column(
                "Lifecycle Status", ColumnIds.LIFECYCLESTATUS,
                type.stringType());

        public static final TextColumnBuilder<String> TYPE = col.column("Type",
                ColumnIds.TYPE, type.stringType());

        public static final TextColumnBuilder<String> 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 StyleBuilder HEADING3 = stl.style().setName("heading3")
                .bold().setFontSize(10);
    }

}

I want to know if my

  1. Naming Scheme is correct
  2. Constant class structure is among best practices.
  3. Should I create separate constant classes or having them encapsulated in a parent Constant class is fine?

Edit: April 19, 2016

I have accpeted an answer but i would like more suggestions anytime.

Thank you.

\$\endgroup\$

migrated from stackoverflow.com Apr 11 '16 at 19:46

This question came from our site for professional and enthusiast programmers.

5
\$\begingroup\$

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 :

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;
\$\endgroup\$
  • 1
    \$\begingroup\$ I agree that enums should be used instead of constants where possible, for type-safety. \$\endgroup\$ – Steffen Harbich Apr 8 '16 at 11:23
  • \$\begingroup\$ I have heard enums take lots of memory space. is this ok to use that many ENUMS? \$\endgroup\$ – Muneeb Mirza Apr 10 '16 at 10:42
  • \$\begingroup\$ Yes that's true, enum does take more memory than constants but with benefits. Following statement will help to put light on it : All enum objects are singletons. This is guaranteed by Java language – you can create an instance of enum only by its definition. This means that you pay for enum object once and then you only spend 4 bytes per its reference (in this article I will assume that object references occupy 4 bytes). Refer following URL for more details on enum : java-performance.info/memory-consumption-of-java-data-types-1 \$\endgroup\$ – Suyash Apr 11 '16 at 4:37
  • \$\begingroup\$ gorbeia.wordpress.com/2015/03/11/java-enums-vs-constants in this link, the last code chunk shows custom enum class with functions that I can use for strings. Is it effecient to have a class that hold hundreds of enums and run function for each single of them? \$\endgroup\$ – Muneeb Mirza Apr 18 '16 at 5:45
  • \$\begingroup\$ It depends on implementation. toString can be used if you want to display different contents from what is present in given enum, otherwise you can use valueOf() or name() which displays contents which are actually present in enum. For ex, in case of enum PLUS("+"), if you use PLUS.valueOf or PLUS.name then you can only get "+" as an output but if you want some custom display then you should use overridden toString(). For many enums, you could always create a map from string to value statically so you only need to map it once, assuming that the returned string remains the same over time. \$\endgroup\$ – Suyash Apr 18 '16 at 8:11
0
\$\begingroup\$

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

https://stackoverflow.com/questions/479565/how-do-you-define-a-class-of-constants-in-java has a Jon Skeet Answer, which recommends against a single separate Constant class:

If the constants aren't related to each other at all, why do you want to collect them together? Put each constant in the class which it's most closely related to.

Putting them inside the class, as you did, seems fine, though.

Re 1: looks good

Re 2: ???

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.