-1

For the below TypeAndSize class,

class TypeAndSize {

  Species type;               // runType EMPTY, SHARK, or FISH
  int size;                   // Number of cells in the run for that runType.

  enum Species{EMPTY,SHARK,FISH}

  /**
   *  Constructor for a TypeAndSize of specified species and run length.
   *  @param species is Ocean.EMPTY, Ocean.SHARK, or Ocean.FISH.
   *  @param runLength is the number of identical cells in this run.
   *  @return the newly constructed Critter.
   */

  TypeAndSize(Species species, int runLength) {
    if (species == null)    {   
      System.out.println("TypeAndSize Error:  Illegal species.");
      System.exit(1);
    }
    if (runLength < 1) {
      System.out.println("TypeAndSize Error:  runLength must be at least 1.");
      System.exit(1);
    }
    this.type = species;
    this.size = runLength;
  }

}

with the usage of member fields of enum class type in below code,

class RunLengthEncoding {
    ...
    public RunLengthEncoding(int i, int j, int starveTime) {
          this.list = new DList2();
          this.list.insertFront(TypeAndSize.Species.EMPTY, i*j);
          ....
      }
    ...
}

let me ask this question.

My question: Why member field of enum class designed to be an instance of enum? Because of the ease in passing on the parameters of enum class type which can be backward compatible with old concept of enum in C language which was a collection of constants? Is that the reason?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • I can't quite understand what you are saying. Are you comparing java's enums to the C-style everything visible type (just typing `EMPTY`)? – Pokechu22 Nov 03 '14 at 00:58
  • There was no `enum` pre 5.0 – Elliott Frisch Nov 03 '14 at 00:59
  • @Pokechu22 yes that is correct – overexchange Nov 03 '14 at 01:02
  • Basically it's type safety, which constants in C (which are just ints) don't provide. – markspace Nov 03 '14 at 01:23
  • you are asking "Why member field of enum class designed to be an instance of enum" but in your code I cannot see anything that you are trying to demonstrate what you are trying to ask. – Adrian Shum Nov 03 '14 at 01:41
  • @AdrianShum am assuming `TypeAndSize.Species.EMPTY` is again of type `Species` here, when i try interpreting member field of `enum` class is again an instance of `enum`. Because name of enum class is `Species`. Is my understanding correct? – overexchange Nov 03 '14 at 01:50
  • @overexchange we don't call `Species.EMPTY` a member field (member field normally implies a instance variable). you can think `enum Foo {A,B}` being a special way to write `class Foo{public static final Foo A = new Foo(); public static final Foo B = new Foo();}` (Of course there are still a lot of difference but in the aspect you are unclear of, it kind of give you some idea on what's happening) – Adrian Shum Nov 03 '14 at 01:53
  • and, next time, please write short and self contained code to demonstrate your idea when you are asking question. There are simply too much unnecessary noise in your code now – Adrian Shum Nov 03 '14 at 01:55

2 Answers2

2

What you are talking about (TypeAndSize.Species.EMPTY) is not a member field of Spicies. When we talk about "member field", it normally implies instance variable (for which it is also possible to write in enum in Java).

In the aspect you are asking, you can simply interpret enum being a special shorthand of writing a special class with class constants:

enum Foo {
  A,
  B;
}

is similar to

class Foo {
    public static final Foo A = new Foo();
    public static final Foo B = new Foo();

    private Foo() {}
}

(There are still a lot of difference between enum and the handcrafted class, but they are not yet your concern)

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • So, `EMPTY` is static filed of `enum` class, it is allowed to access that static member using reference variable `Species` – overexchange Nov 03 '14 at 02:22
  • i think i did not get your answer, are you saying, `TypeAndSize.Species.EMPTY` can be passed to a variable of type `Species`? – overexchange Nov 03 '14 at 02:38
  • `EMPTY`, in your example, is a `Species`. Make sure you understand what I have wrote: `A` and `B` are final static member of `Foo`, of type `Foo`. If it is of type `Foo`, of course it can be used as a `Foo` (to pass to method as argument of type `Foo` for example) – Adrian Shum Nov 03 '14 at 06:37
1

with the usage of member fields of enum class type

TypeAndSize.Species.EMPTY is not using a member field. It is access the enum type defined in TypeAndSize.

Species type is defining a member field but it's type isn't enum, it's type is Species.

Enum is on the same level as class and interface. These are the building block types we can use. The reason that we want to use enums over integer constants can be read elsewhere.

Hopefully the link link helps answer the second part of your question.
I've tried my best to clear up the terms used in the question.

Community
  • 1
  • 1
pimaster
  • 1,907
  • 10
  • 11
  • what i meant is, `TypeAndSize.Species.EMPTY` or `TypeAndSize.Species.FISH` are of type `Species`. is that correct? i said `enum class type` in my query, but not `enum` and the name of `enum class type` is `Species`. – overexchange Nov 03 '14 at 06:29
  • Yes, `Species.EMTPY` and `Species.FISH` are values of type `Species`. You have a type Species which is an enum, "the name of enum class type is Species" seems a round about way to say it. – pimaster Nov 04 '14 at 01:07