4

My textbook barely talks about enumeration in Java, and the videos that I've watched don't explain much. So from what I'm understanding, enumeration is like a whole different class where you can store constants in. Can someone expand to me about the constants and perhaps show me better examples? Like I understand what constants are after seeing the examples such as colors, directions, and in the previous videos it was people, while in the enum version of one my projects during the school year, it was command words. But I don't 100% understand the concept or how to use.

  1. Also, what's the point of an Enumeration when you can just make a collection? Like for instance, the last video i saw, the video maker made enums of people in the format of name(String description, int age), and that's how he defined his constructor and he had get and set methods. What's the advantage of doing this rather than just creating a person object in the same exact way and them creating a collection and storing person objects in there?

  2. I went to look up the above, and after seeing this thread: Difference between Java Enumeration and Iterator An iterator is something that will let me loop through a collection, and all this time I thought enumeration was something like a different class. But in the thread they're comparing them. Enumeration is just something like an iterator, but without the remove method. Is this enumeration something different than what I was talking about above?

Community
  • 1
  • 1
Honinbo Shusaku
  • 1,411
  • 2
  • 27
  • 45
  • 1
    Go read the javadoc for `Iterator` and `Enumeration`. – Sotirios Delimanolis Dec 20 '13 at 16:13
  • And an "enum" is not the same as an "Enumeration". – Elliott Frisch Dec 20 '13 at 16:15
  • 3
    You are getting confused between an `enum` and an `Enumeration`. An `enum` is a special type of singleton class whereas an `Enumeration` is a deprecated `Iterator` pattern. Forget about `Enumeration`. – Boris the Spider Dec 20 '13 at 16:15
  • 1
    Whenever you have `public static int SOMETHING=0; public static int SOMETHINGELSE=1;` etc and then use `SOMETHING` completely seperately from the `0` it's stored as you can formalise that as an enum – Richard Tingle Dec 20 '13 at 16:18
  • I read enum types before, and I just looked over it, and I'm still unsure of when to use over a collection, as I don't seem to understand it. And I've read iterator before and right now as well. The java docs for enumeration is very much like the one for iterator, but that's where I'm confused...is enumeration at all related to enum types? – Honinbo Shusaku Dec 20 '13 at 16:18
  • 1
    @Abdul No, `Enumeration` is not related to `enum`. As Boris said, forget about `Enumeration`, as `Iterator` has essentially replaced it. As for `enum` vs. `Collection`s, an `enum` is better used for static, constant data, whereas a `Collection` is better for dynamically produced and/or changing data. – ajp15243 Dec 20 '13 at 16:20
  • So, if I have a bunch of things who's value isn't going to be changed, and that I want to be used universally (that's why it's static?) I'd use an enum. Am I correct? – Honinbo Shusaku Dec 20 '13 at 16:24
  • 1
    @Abdul More or less. An `enum` is useful for, as Boris' answer basically says, enumerating constants. It is useful for containing a [set](https://en.wikipedia.org/wiki/Set_(mathematics)) of distinct but related constants that are [enumerated](https://en.wikipedia.org/wiki/Enumerate). So an `enum` for the cardinal compass directions (North, South, East, West) would be a good use case. The scope (universal or otherwise) would depend on your application design. I feel that my explanation may be a bit lacking. Does it make sense? – ajp15243 Dec 20 '13 at 16:36
  • There was something else I was wondering an hour ago, but unfortunately I forgot, but your explanation as well as Boris' is clear and helpful. Thank you. Unfortunately I can't add to rep as mine isn't 15 yet. – Honinbo Shusaku Dec 20 '13 at 17:32

1 Answers1

6

You are confused between several different classes.

  1. enum
  2. Enumeration
  3. Iterator

An enum is an enumerated constant, i.e. a constant that can take several defined values such as

public enum Gender {
    MALE,
    FEMALE;
}

It is designed to provide type safety.

An Enumeration is a now deprecated part of the Collections API - forget about this. It is superseded by Iterator.

An Iterator is an implementation of the Iterator Pattern as described by the Gang of Four.

For why to use an Iterator rather than a Collection maybe my answer here will help.

As for enums of people in the format of name(String description, int age), and that's how he defined his constructor and he had get and set methods. This is a big no-no.

An enum should be a constant so should not have setter methods. An enum is a set of defined values like in my example above.

If you want a Collection of people then a Person class in a Collection<Person> would be the correct solution.

So, in summary. Use an enum for constant values; use a Collection for, well, collections of things. And do not use an Enumeration - forget it exists at all.

Community
  • 1
  • 1
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • 1
    I think the last point to address is confusion between use cases for `enum` vs `Collection`. – ajp15243 Dec 20 '13 at 16:22
  • when you say type safety, it's because you don't have to define what types the constant are in an enum correct? – Honinbo Shusaku Dec 20 '13 at 16:27
  • 2
    @Abdul if I have a method `setGender(final int gender)` I can pass in any valid `int` - say `2,000`. This means it is up to the program to check validity. If I have a method `setGender(final Gender gender)` I can **only** pass in `Gender.MALE` or `Gender.FEMALE` - I have turned `Gender` into its own **type**. This is type safety. – Boris the Spider Dec 20 '13 at 16:43
  • ohhhh I see. You're making your own data type. Thank you. – Honinbo Shusaku Dec 20 '13 at 17:30
  • 1
    @Abdul please accept the answer if it answered your question. Thanks. – Boris the Spider Dec 20 '13 at 19:42