0

What is the overall advantage (in terms of memory management, etc.) of creating a separate constants file, like below class "Constants", over directly writing in Java file like stringVar.equals("name")?

public final class Constants {
    // Hide the constructor
    private Constants(){}
    public static String NAME="name";
}

I know that even if we write use "name" 2 times (as below), only one literal will be created in String pool?

stringVar1.equals("name") and stringVar2.equals("name")

talemyn
  • 7,822
  • 4
  • 31
  • 52
kumar
  • 708
  • 4
  • 15
  • 39
  • 10
    This kind of thing isn't done for memory management reasons. – user2357112 Feb 17 '17 at 19:07
  • 2
    A constants-only class is an antipattern. Don’t do it. Instead, put your constants in the class to which they pertain. For instance, [Pattern.DOTALL](http://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html#DOTALL), not PatternConstants.DOTALL. – VGR Feb 17 '17 at 20:41

3 Answers3

3

One reason would be to reuse it (and as a consequence prevent bugs from typos).

If you are using an IDE, it also becomes easier to perform a usage-lookup in a bit more non-ambiguous manner than plain string search.

The other as already noted in the comment, would be memory but as I see it, the above two reasons are far more significant - in general usage. My bad. Looks like memory has not much to do with it - atleast in case of static-final fields .

Community
  • 1
  • 1
Ravindra HV
  • 2,558
  • 1
  • 17
  • 26
  • I want to upvote this except for the last paragraph. It has nothing to do with memory, and virtually no effect on it. It's sole effect is on organization and maintainability. Definitely don't want beginning Java programmers to start throwing all their constants in a dedicated file in inappropriate situations because they think it somehow affects performance! – Jason C Feb 17 '17 at 19:14
  • It is so ugly it is considered an antipattern [https://stackoverflow.com/questions/22868084/if-the-constant-interface-anti-pattern-is-such-a-crime-why-does-swing-do-it]. – Dave Apr 23 '18 at 05:44
  • 1
    @Dave that is about using interfaces (and implementing them) only for importing constants. But this question is about a class. – Holger Oct 20 '22 at 12:02
  • It is ugly - and considered an anti-pattern - for the same reason. It keeps constants in a class removed from their relevance and context. There is zero value in creating a class to contain global constants. This is classic C-coder come Java dev ugliness. Constants belong in the class to which they pertain. – Dave Nov 03 '22 at 08:20
  • The whole idea of a global constant would be consistency. The same string literal even in the form of a local constant repeated across codebase would defeat the purpose of a constant. – Ravindra HV Nov 03 '22 at 16:56
  • @RavindraHV String literals are automatically interned constants in Java. If you use the constant elsewhere in the codebase you dereference it with its enclosing class or interface name. If the same string referenced in two places embodies two different things that are only coincidentally the same value they should be two constants. You are confusing constant with scoped literal. The purpose of a constant is to inform the compiler that the value will not change. The purpose of extracting a literal to a class constant is to avoid accidental typos and lower maintenance effort. – Dave Feb 28 '23 at 00:59
2

It's not about efficiency; it's about maintainability.

The constant both defines the value "name" in a single place, and associates a constant name (you chose NAME) with it.

In your example, the constant name doesn't tell us anything. But it could be, for example, public static String FIRST_NAME_FIELD = "name". Then it would be more clear, both in the Constants class and at every usage, what the string "name" means.

And, of course, if you need to change "name" to "first_name" later, it's easier.

Justin
  • 655
  • 8
  • 17
1

Performance wise, java tend to reuse Strings in the String pool.

Constants are usually used when you know the value won't change or must not be changed by anyone else.

For a constant you must add final to you variable declaration:

public static final String NAME="name";

Constants are more related to better coding, organization and reuse than to performance.