2 Replies - 14889 Views - Last Post: 23 May 2011 - 04:34 AM Rate Topic: -----

#1 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Enum tricks: hierarchical data structure | Javalobby

Posted 02 February 2011 - 07:30 AM

http://java.dzone.co...erarchical-data

Quote

Java enums are typically used to hold array like data. This tip shows how to use enum for hierarchical structures.


I liked this article very much, since it shows (once again) that there is always a way of using something that you did not yet think of yourself. Inspired by this post I had fun developing this (admittedly not always useful) technique to avoid switch-statements: http://schneide.word...switch-use-enum.

Is This A Good Question/Topic? 3
  • +

Replies To: Enum tricks: hierarchical data structure | Javalobby

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,053
  • Joined: 11-December 07

Re: Enum tricks: hierarchical data structure | Javalobby

Posted 02 February 2011 - 07:58 AM

That's not really a good use for enums but it is good for classes. It's also a standard principle of object orientated programming. You define a method in an interface and then provide classes with different implementations. Then whenever you would have a lengthy if .. elif or switch, you just call the method on the object you have.

You can do this with enums, and occasionally you might want to, but the real reason for enums is in switch statements.
Was This Post Helpful? 0
  • +
  • -

#3 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Enum tricks: hierarchical data structure | Javalobby

Posted 23 May 2011 - 04:34 AM

A rather interesting read, I was actually looking for a good article about this very subject, Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1