0

I have lot of static/constant data which I want to store, this data is also related with each other. I can use lot enums referencing each other forming a tree or a graph. Or simply use tables or database enums and store values in them and create corresponding classes and respective relationships. The data I have is constant and is certainly not going to change. I might have to also consider internationalization in near future. I will be using this constant data as filter to various other data.

I am tempted to use enums as it gives me immutability by default, but seeing the complexity of relationship between data, like I might have to sacrifice with inheritance, I am also little apprehensive of enums. And populating these enum classes from database and internationalization might be little more tricky. And at later stage hoping that it will scale and embrace the complexity with ease are the areas of concern as I would not like to revert from the mid way.!

---Update---

I have not seen examples of enums related(associations) with each other, containing fields of complex types referencing other enums. Can in this type of cases enums replace classes when data is constant. Is there any objective way to look at this problem.

To understand better, I have similar classification like below. Animal Kingdom having tree hierarchy

vaspaean
  • 83
  • 1
  • 13

2 Answers2

1

As a rule of thumb, I only involve a database when the values are likely to change faster than code release cycles, and when it's possible or likely that someone who is not me is going to change them. Making the code depend on a running (and available) database means that when some DBA takes the database down for maintenance then your application can't be started.

Stephen B.
  • 396
  • 2
  • 19
  • Thanks for this very handy tip, it will help me in making the decision. I still need more thoughts on creating structure with constant data tightly associated and referencing other constants with various properties. – vaspaean Aug 11 '18 at 01:30
  • Well, do what you need to do, maybe build a ConcurrentHashMap or something. Ultimately, if it's a constant, then it might as well be a public static final String in some class somewhere. You may find Collections.unmodifiable... useful, as well. – Stephen B. Aug 11 '18 at 03:49
1

While this Question is likely too broad for Stack Overflow, a few thoughts.

Enums

You may not fully understand the enum facility in Java. See the Oracle Tutorial, and see the Enum class doc.

An enum is a class, a regular Java class, a subclass of Enum. The only thing special is that syntactic sugar that automatically instantiates the static instances you define and name. Otherwise, they are normal classe:

  • Your enums can carry member variables.
  • Your enums can have constructors, and you can pass arguments to those constructors.
  • Your enums can offer other methods, and you can pass arguments to those methods.
  • You can even pass instances of one enum as arguments to methods of another enum’s instances – just as you might pass instances of an enum to instances of other non-enum classes. Each enum instance is just an object, plain and simple, saved as a static reference on the enum-defining class.

Example:

public enum Food { HERBIVORE, OMNIVORE, CARNIVORE ; }  // Syntactic sugar for automatically instantiating these named static instances of this class type.

…and…

public enum Animal {
    RABBIT( Food.HERBIVORE ) , 
    DOG( Food.OMNIVORE ) , 
    CAT( Food.CARNIVORE ) ;  

    // Member variables.
    public Food eats ; 

    // Constructor
    Animal( Food foodType ) {
        this.eats = foodType ;  // Assign an instance of another enum to this instance of this enum.
    }

}

Limitations of enums

While more powerful and useful than in other languages, there are limitations.

Compile-time

Firstly, enums are defined at compile-time. If your values change at runtime, perhaps you want to add or delete items, then enums are not appropriate.

Permanently in memory

Also, enums are static. This means when first used, all the objects of that enum class are instantiated immediately and held in memory throughout the execution of your app. So they are never retired from memory until program ends. So having an enormous number of them might be a burden on memory.

Understand that your can collect enum instances. See the EnumSet and EnumMap classes for fast-to-execute and low-memory usage collections of enum instances. Search Stack Overflow for much coverage on this topic. And be aware that every enum carries a values() method that returns an array of its values, yet this method is mysteriously not listed in the JavaDoc.

As for your mention inheritance, your enums by definition are subclasses of Enum class. So they cannot inherit from any other class you may have in mind, as Java does not support multiple-inheritance. Your enums can implement one or more interfaces. In later version of Java, an inheritance can carry implementation code by way of new default methods, so you can pass along some code that way.

Internationalization

Internationalization and localization seems to be an orthogonal issue. You can add a method on your enum to generate localized String representation of their value. As an example, see DayOfWeek::getDisplayName and Month::getDisplayName enum methods.

Database

If you want to dynamically define your values at runtime, or you have zillions of them, then a database is the way to go. A serious database such as Postgres is designed to manage memory, handle concurrency, and execute efficiently.

You can even combine enums with the database. For example, localization. You might have enum values defined at compile-time, but their getDisplayName method does a lookup into a database to find the French or Arabic translation. That translation value in the database can be updated during runtime by running SQL INSERT or UPDATE commands via JDBC.

Recursive hierarchical relationships

If you are trying to represent relationships of a hierarchy of arbitrary depth, that is a whole other topic I'll not address here other than to say that is often implemented with recursion. Search Stack Overflow and other sources to learn more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks for nice info on enums. If constant data relates with each other with various association and linkages. should it be persisted in tables.? I have not seen examples of enums related with each other or containing fields of complex types referencing other enums. Can enums replace classes when data is constant ? – vaspaean Aug 11 '18 at 01:20
  • See new third paragraph. You can pass instances of one enum to instances of another enum, thereby associating them. And you can collect them, another way of associating them. – Basil Bourque Aug 11 '18 at 01:49
  • Thanks for this nice insight into enum. I think it.might work this way. – vaspaean Aug 11 '18 at 10:07