Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (777)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
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 250 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?

-FabulousFellini
www.fabulousfellini.com
Offline KaiHH

JGO Kernel


Medals: 650



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

See: https://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel

Of this, this is my favorite answer: https://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel#answer-26118954

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

-FabulousFellini
www.fabulousfellini.com
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

nelsongames (2247 views)
2018-04-24 18:14:32
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

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org 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‑gaming.org
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!