Tuesday, 27 September 2011

Java: Enumerated Types


Update 1/2 for today. Update 2 auto-posting in like....12 hours.

Very often, we'll want to represent concepts along the lines of "suits in a pack of cards", "days of the week", "pieces in a game of chess". These share something in common, namely that these concepts are types with a small, fixed number of values. (4 suits of cards, 6 types of chess pieces and 7 days of the week). This is the sort of thing described by the term enumerated type. A long time ago (and indeed, how I thought it would be done before reading about it just now), people would represent these types by creating a class for each. Well, no more!

Here's an example of creating an enumerated type for the card types, and writing a toString() method to overwrite the default one:

Click me!
Note the highlighted line, this isn't a class, you cannot instantiate it! We'll look at using an enum class in the next post! Right after you declare the enumerated type, the first part of the body should consist of all those types, separated by a comma. The semicolon denotes the end of the types. Then, we have any methods! You remember switch statements, right? Well, if not, that's okay, tomorrow, we'll be done with the chapter as it appears in the book I'm reading, so it'll be time for a review/summary/whatever. Anyway, while this method works, it kinda doesn't feel so hot. Mostly,I just don't use switch a lot, maybe I'm just not too comfortable around it, even though its way easier. Luckily for me and my silliness, the people developing Java thought to allow each value to define its own methods. This opens up the way for us to do the following: 


This might look a hell of a lot worse, and I can see how. You need to repeat the method name each time, it looks uglier and longer, but keep in mind, you won't always be working with card suits, or toString() functions. The toString() could probably stay as a general method, anyway, but say you had a video-game with several different types of spaceship, you don't want a general function for something that only 1 of them can do, right?

Note that you still need to separate each enumerated type by a comma!

Next post will be largely examples of putting this to use within a class, but first wrap your head around this general idea.

Questions welcome! Comment, follow, subscribe, share etc,


And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here (fixed), and feel free to throw loads of criticism at us. 

8 comments:

  1. This is very interesting. I'm trying to pick up Java and I need code like to this to experiment with so I at least know what it does when I stuff around with it.

    ReplyDelete
  2. Really good, I love the notes you put in. And what? There's auto-posting?

    ReplyDelete
  3. this made little sense to me.

    ReplyDelete
  4. Java looks a lot like the language that runs ArmA when you're modding it.

    ReplyDelete
  5. Yep, when writing a post, on the right-hand side is a Schedule option.

    ReplyDelete
  6. hahaha man, give your friend a better shout out. That's almost invisible.

    ReplyDelete
  7. @Plumber: Yeah, I should, I guess :x
    @Pesos: Yes, you can prepare a post ahead of time and choose when you want it to go live.

    ReplyDelete