jichoul shin
jichoul shin

Reputation: 71

Why use constant in java?

public class MyConstant {
   public static final String COMMA = ",";
   public static final String EMPTY = "";
}

I learned using constant the reason why dont use permgen memory and removing repeatability and clean code.
And i heard java8 about removed permgen memory.
So java8 still need constant without repeatability and clean code?
Or have another reason why use constant?

Upvotes: 1

Views: 176

Answers (1)

Jim Garrison
Jim Garrison

Reputation: 86774

You use named constants (and enums) in Java for at least two and probably more reasons:

  1. Constant values used in your program are given meaningful names This avoids the use of "magic numbers" in code that give future maintainers fits and cause them to curse your name forever.
  2. A value may be a constant when the program was written but might change in the future when the program is revised for new requirements. Would you rather change a single constant in one file, or the 1000 places in the code where that constant appears.

Upvotes: 3

Related Questions