4

Let's say I have a constants class containing 200+ static fields :

class AnimalConstants {
  static final int AARDVARK = 1;
  static final int ANTELOPE = 2;
  static final int BEAR = 3;
  ...
  ...
  static final int ZEBRA = 200;
}
  • Can anyone explain if there are any negative impact on performance and memory from using such classes.
  • Would it be better or worse if the class is changed to an interface (e.g. SwingConstants) and being implemented by some classes?
  • Would it be better or worse if I implement the constants as an Enum class?
t1w
  • 1,408
  • 5
  • 25
  • 55
Augustus Thoo
  • 492
  • 4
  • 13
  • 2
    Seeing your use case, perhaps you should look into `enum`s – John Dvorak Jan 10 '13 at 10:13
  • 2
    Don't even thing about performance until you have the written an easy to maintain version first and you have profiled your application to determine you have a performance problem. Even if you have 20 years experience performance tuning systems you should still measure, not guess. – Peter Lawrey Jan 10 '13 at 10:21

6 Answers6

7

I don't think the impact is performance or memory.

I think it has to do with things like readability of code, keeping constants close to where they're used, and fundamental understanding of your problem.

I prefer to declare constants closer to where they're used. I believe they're easier to understand that way.

I would be surprised if the real constants that you claim number 200+ are truly related. If they are, they belong together in one place. If not, I'd say that they should be broken into smaller pieces and declared closer to where they're used.

I'll bet there's more context than your contrived example that would change responses if known.

Sure , enums are great. But see my other comments first.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • So, I should not be too worried about using large Constants then? I use constants and enums in my projects. About 80%-20%. – Augustus Thoo Jan 10 '13 at 10:20
  • "So I should not be too worried" - where did you get that? Yes, I think you should be worried. I don't believe your use case demands 200 constants. – duffymo Jan 10 '13 at 10:29
  • Actually, I am working on a trading API project that sparked my question posting. The AnimalsConstants is just an abstraction to help with my question. – Augustus Thoo Jan 10 '13 at 10:34
  • 1
    Are the 200+ constants trading symbols? They belong in a database, not an Enum or interface. – duffymo Jan 10 '13 at 10:54
2

Of course enum implementation is more ponderous than bunch of int constants but using enum:

  1. you don't need to hardcode actual values of Animals (in your case) that can change later
  2. you don't need to hardcode total number of Animals and you can simply iterate through all animals
  3. methods with parameter of this enum will be understood correctly (foo(Animal animal) is better than foo(int animal))
  4. you can add additional functionality to your enum values later, e.g. internal value isMammal
bellum
  • 3,642
  • 1
  • 17
  • 22
2

Would it be better or worse if the class is changed to an interface (e.g. SwingConstants) and being implemented by some classes?

--> That would be a Constant Interface Pattern. If we use interfaces for constant and it is implemented by all classes but if you are developing an API, it is something like you are exposing your implementation details. Above wiki link explains this very well.

In both approach(Interface or Class) I would suggest using final class, create constants and do static import for constants wherever necessary.

Would it be better or worse if I implement the constants as an Enum class?

--> With Enums, this would be the best approach.

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
2

Changing any value that has already been compiled into another class may require a full build.

Addendum: See Is it possible to disable javac's inlining of static final variables? for a more thorough examination.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Yes it is okay to create a large number of constants. It is hard to discuss negative impact because we don't know any alternatives because we don't have your functional requirements.

But be assured that the compiler is written to work well with code written by humans. Having a bunch of fields is probably going to be okay.

I feel that constants can be very nice as it can be used in switch case since JDK7, you can compare with == and the variable name can be informative.

Can enum be even better? Yes it can. Explore the features of enums and see if anything is appealing to you

Karl Kildén
  • 2,415
  • 22
  • 34
0

For your kind of vars (Animal Types) i suggest you to use an Enumerator instead of a class. With the number of vars using it shouldn't be a problem for performance as you're only using int primitive. The problem would have occurred if any var has been a class, that are more memory demanding to maintain their structure. I hope to have clarified your doubt (Sorry for the poor english, i'm a little rusted)

Slash87
  • 11
  • 2