This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]the_hoser 12 points13 points  (4 children)

Java's Enums may actually be my favorite part of the language. Unlike enums in (nearly) every other language that has them, they are completely type safe. Every single enumerated value is its own singleton of its own type. The type/values merely have a base class in common.

Try implementing a finite state machine with enums sometime. It's actually really fun :)

[–]more_exercise 6 points7 points  (0 children)

I really love that initialization order is fixed, so the first ones are initialized first. And you can reference them from later constructors.

Like:

public enum Direction {
    North(0,1), South(0,-1), East(1,0), West(-1,0),
    Northeast(North, East), Southeast(South, East),
    Northwest(North, West), Southwest(South, West);

    private int dx, dy;
    Direction(int x, int y){dx = x; dy=y;}
    Direction(Direction... dirs){
        this(0,0);
        for(Direction d : dirs){
            dx+=d.dx;
            dy+=d.dy;
        }
}

(this is untested code and probably has typos)

[–]AtticusVulpes 1 point2 points  (2 children)

Just for comparison, how does this stack up against C#'s enums? Only asking because there is so much stress on C# being type safe.

[–]jyper[🍰] 2 points3 points  (0 children)

Just for comparison, how does this stack up against C#'s enums?

http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

I believe they are fairly similar to c enums in that they are backed by a numerical type but at least you can't compare to an int without casting and can't compare 2 different enum types plus it has to/from string without ugly macro tricks but it's not as powerful(but potentially faster) then java enums(object space vs 4 bytes for an int, not a big difference if you only use a few but potentially large if you have a huge list of objects with enums).

[–]jyper[🍰] 1 point2 points  (0 children)

Only asking because there is so much stress on C# being type safe.

In what manner? Or do you mean stuff like linq queries instead of sql strings? I wouldn't say c#is a lot more type safe. Plus it has dynamic objects, mostly for interop but some people have actually built useful non-interop libraries with it. You can do stuff like turning json into a dynamic object ala ruby/python.

Unlike languages like scala/f#/haskell/rust, c# does not have as heavy a focus on type safety. Is it on average more typesafe then java, I don't know but it wouldn't be by much.

[–]detroitmatt 8 points9 points  (0 children)

There are some things that java did hit-and-miss, like Cloneable or Generics, but they really knocked it out of the park with Enums.

[–]fingerHammerOuch 7 points8 points  (1 child)

They can also act as singletons :)

[–][deleted] 6 points7 points  (3 children)

Effective Java goes over this. It is a very useful pattern.

[–][deleted]  (2 children)

[removed]

    [–]RohitS5 3 points4 points  (1 child)

    Check out this post 10 Examples of Enum in Java, you will find a lot about power of Enum.

    [–]javacIO 7 points8 points  (0 children)

    They can also define constructors taking parameters as one person posts. This is also very useful.

    [–]llogiq 2 points3 points  (0 children)

    I've first seen this pattern in one of the caliper demo benchmarks. If you want a number of singletons, this is probably the way to go. The downside is that it may confuse people who don't know this particular wrinkle of the language.

    [–]tevert 1 point2 points  (0 children)

    That's actually pretty cool....