2

In the book Effective Java its told that:

The basic idea behind Java’s enum types is simple: they are classes that export one instance for each enumeration constant via a public static final field. Enum types are effectively final, by virtue of having no accessible constructors.

So, if I have an enum like:

public enum Orange {NAVEL, TEMPLE, BLOOD}

Then, it must be similar to:

public class Orange implements Comparable , implements Serializable {
    private Orange() {}

    public static final Orange NAVEL = new Orange();
    public static final Orange TEMPLE = new Orange();
    public static final Orange BLOOD = new Orange();

    public int compareTo(Orange other) {}
}

To this point it seems fine, but after some time the examples get over complicated, for example I am still puzzled over the following enum definition.

public enum Ensemble {
    SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5),
    SEXTET(6), SEPTET(7), OCTET(8), DOUBLE_QUARTET(8),
    NONET(9), DECTET(10), TRIPLE_QUARTET(12);

    private final int numberOfMusicians;

    Ensemble(int size) {
        this.numberOfMusicians = size;
    }

    public int numberOfMusicians() {
        return numberOfMusicians;
    }
}

What could be its similar class equivalent and is there a length property on it?

6
  • You can list all values of your enum using values static method, which returns an array. It hten provides length property. Commented Nov 3, 2015 at 8:55
  • @OndrejM an example will never hurt anyone :)
    – vivek
    Commented Nov 4, 2015 at 4:32
  • 1
    I wanted to encourage you to study :) Anyway, an example goes like this: Ensemble.values().length Commented Nov 4, 2015 at 5:54
  • 2
    This is off-topic and probably more suitable for StackOverflow (where it could get closed anyway), but it's too old to migrate.
    – Andres F.
    Commented Feb 24, 2016 at 5:00
  • 1
    @AndresF. I disagree. While it does have code in it, the goal is to understand how enums are similar to "regular" classes in Java at a conceptual level. The compiler makes enums inherit from Enum and adds some syntactical sugar and boilerplate code to the class definition. This is not very intuitive to a beginner who is diving into using enums without reading all of the (unnecessary) documentation on them.
    – user22815
    Commented Feb 24, 2016 at 5:39

1 Answer 1

5

The latter example corresponds to a class like this:

public final class Ensemble {
    public static final Ensemble SOLO = new Ensemble(1);
    public static final Ensemble DUET = new Ensemble(2);
    public static final Ensemble TRIO = new Ensemble(3);
    ...
    public static final Ensemble TRIPLE_QUARTET = new Ensemble(12);

    private final int numberOfMusicians;

    private Ensemble(int size) {
        this.numberOfMusicians = size;
    }

    public int numberOfMusicians() {
        return numberOfMusicians;
    }
}
4
  • So, I need to have final keyword too while emulating enums since it can't be inherited, right?
    – vivek
    Commented Nov 4, 2015 at 4:31
  • 1
    Correct. The reason for making the class final is that enums can't be extended. There are some features of enums which are not covered in this example: enums are Comparable, they provide a static values method etc. So, if you really want to write a class that emulates an enum thoroughly, more needs to be done. This example covers only the use of a private constructor for creating constants.
    – COME FROM
    Commented Nov 4, 2015 at 15:53
  • Update: having private constructor also prevents sub-classing why we need final then?
    – vivek
    Commented Nov 18, 2015 at 6:36
  • 2
    @vivek Yes, if a class doesn't have any accessible constructors, then it is effectively final. However, it's good practice to communicate your design choices explicitly. The Java compiler doesn't really need final here but we as programmers need final to make it clear that the class we've written is not supposed to be sub-classed. Trying to extend a non-final class that has no visible constructors can be slightly confusing. The compiler will not tell you that the class can't be sub-classed but rather advice you to do something that's really not possible. Try it out.
    – COME FROM
    Commented Nov 18, 2015 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.