4

Is this a good idea, to group constants in classes inside Constants class container like

public final class Constants {
  public final class File {
    public static final int MIN_ROWS = 1;
    public static final int MAX_ROWS = 1000;

    private File() {}
  }

  public final class DB {
    public static final String name = "oups";

    public final class Connection() {
      public static final String URL = "jdbc:tra-ta-ta";
      public static final String USER = "testUser";
      public static final String PASSWORD = "testPassword";

      private Connection() {}
    }

    private DB() {}
  }

  private Constants() {}
}

It allows to use Constants.DB.Connection.URL instead of DbConnectionConstants.URL.

3
  • Grouping sometimes helps with clarity. public class Constants { private static final class KeyOffsets { public static final int QTY = 0; //8-bytes (long) public static final int PRICE = 8; //8-bytes (long) } public static class Offset { public static final int Action = 16; } }
    – user982733
    Jan 10, 2012 at 20:21
  • Possible dup of stackoverflow.com/questions/66066/…
    – JustinKSU
    Jan 10, 2012 at 20:41
  • AS I PREFER NEITHER MY CODE SHOUTING AT ME NOR PROGRAMMING IN COBOL, I TEND TO USE PascalCase FOR MY CONSTANTS!!!!!!!
    – yeoman
    Oct 24, 2018 at 12:41

5 Answers 5

9

I generally prefer to put constants in the class where they belong. For example, the file constants could be in FileManager (or something like that), where they are used. The connection constants could be in your DBUtil class, where they are used.

Think about the JDK. Does it have a gigantic Constants class? No. The constants used by (and with) BorderLayout are in the class BorderLayout. The constants used by (and with) JOptionPane are in JOptionPane.

1
  • If a constant is only used once in a centralized place, I agree with this approach. However, it might not work in more complicated implementations.
    – JustinKSU
    Jan 10, 2012 at 20:16
2

Advantages:

  1. You get a nice "path" to your constants.
  2. You might get a cross-code constants convention, if colleages follow the guidance.

Disadvantages:

  1. With many constants and many "domains" of constants, that class might get too cumbersome to handle.
  2. It's not intuitive. It reminds me more of Ant properties than java constants.
  3. It prevents modularization of your code. Say you want to separate your DB connecting package and File handling package to different jars which might be used by different applications. It is redundant to deliver your DB management jar with file handling constants. And you can easily get to have many such modules.

In conclusion, it's something that might be handy when you write just some little application for your own self. Generally, it is not a good practice.

1

Use Enum http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

each JVM will have only one copy of enum object

4
  • How do you store a database server password in an enum? Jan 10, 2012 at 20:41
  • INSTANCE;public final PASSWORD="abcd";
    – ligerdave
    Jan 11, 2012 at 2:33
  • Even if that would work, it's an abuse of an enum. Further, your argument suggests that constant Strings aren't interned by the JVM, which they are. Jan 11, 2012 at 14:58
  • you are correct. there will be only one copy of the String object. using enum is just a way to encapsulate things. what do you think about using enum as a way to implement singleton pattern? do you think that's an abuse of usage?
    – ligerdave
    Jan 11, 2012 at 21:17
1

You could always use a .properties file in the classpath using the foo.bar.baz notation. This would make it easier to change the values without recompiling. The Spring framework provides utilities to help refresh the values on the fly.

However, I agree with @JB Nizet and @yair that keeping with with the Class most associated with the constant is a better idea.

-3

You can use abstract for the constants class too, even better than the private constructor. If you use an interface it would be usable outside the class hierarchy and one may simply use implements though hiding the origin (interface name).

One issue though. If you import a class only for a constant, the compiler may remove the import in the .class and fill in the constant itself. If one later changes the constant's value, the using class will not automatically recompile, and maintain the old value.

2
  • A constant interface (or abstract class) is generally considered as an anti-pattern. Static imports have been introduced to be able to have the same advantages without the antipattern. Your remark about imports makes no sense at all. Imports are only used at compile time. They aren't in the class file. Whether you import or not, a constant is always inlined.
    – JB Nizet
    Jan 10, 2012 at 20:29
  • @JBNizet yes interface is an anti-pattern, therefore "though hiding the origin." On imports: imported class names appear normally in the constant pool of a compiled class; not so for the above case. But agreed, I would call that a compiler specific bug.
    – Joop Eggen
    Jan 10, 2012 at 20:41

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