Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (790)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Enum Vs Constants  (Read 908 times)
0 Members and 1 Guest are viewing this topic.
Offline FabulousFellini
« Posted 2018-12-03 19:19:42 »

This has nothing to do with game programming, more just best java practices.  Besides maybe readability, is there an advantage of using enums over finals/constants?

Offline KaiHH

JGO Kernel

Medals: 689

« Reply #1 - Posted 2018-12-03 19:33:40 »


Of this, this is my favorite answer:

However, if you use an actual class as the type for your static final fields, then the only real advantages of enums over static final fields are:
4. Ability to use values in switch statement case statements without qualification (so without having to assign an ordinal manually to each value of a static final field and projecting that number for the switch)
5. Built-in sequentialization of values via ordinal()
6. Serialization by name not by value, which offers a degree of future-proofing (obviously depends on which serialization mechanism you actually use - I think they mean the standard Java serialization here)

When using classes as the types of static final fields, the following are no real advantages of enums anymore:
1. Type safety and value safety
3. Ability to define and override methods
7. EnumSet and EnumMap (just use standard IdentityHashSet and IdentityHashMap)

Another advantage of enums is of course the ability to iterate over all enum literals, which you cannot do easily or efficiently with static final fields (i.e. Reflection or putting the field values in a custom collection).

Now, which way you choose just simply depends on what you want to do with the enum (or static final fields). For simple int/long values that require no further typedness and where you really only need the ordinal number, then static final int/long fields are good.
Offline FabulousFellini
« Reply #2 - Posted 2018-12-03 19:45:59 »

Ok cool that sums it up well!  Thanks for the detailed answer Smiley

Pages: [1]
  ignore  |  Print  

hadezbladez (2843 views)
2018-11-16 13:46:03

hadezbladez (1026 views)
2018-11-16 13:41:33

hadezbladez (2810 views)
2018-11-16 13:35:35

hadezbladez (551 views)
2018-11-16 13:32:03

EgonOlsen (3822 views)
2018-06-10 19:43:48

EgonOlsen (4235 views)
2018-06-10 19:43:44

EgonOlsen (2543 views)
2018-06-10 19:43:20

DesertCoockie (3355 views)
2018-05-13 18:23:11

nelsongames (3469 views)
2018-04-24 18:15:36

nelsongames (4467 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!