Friday, 15 May 2015

Why instance field of 'enum' is 'enum' in java? -



Why instance field of 'enum' is 'enum' in java? -

for below typeandsize class,

class typeandsize { species type; // runtype empty, shark, or fish int size; // number of cells in run runtype. enum species{empty,shark,fish} /** * constructor typeandsize of specified species , run length. * @param species ocean.empty, ocean.shark, or ocean.fish. * @param runlength number of identical cells in run. * @return 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 @ to the lowest degree 1."); system.exit(1); } this.type = species; this.size = runlength; } }

with usage of fellow 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 inquire question.

my question: why fellow member field of enum class designed instance of enum? because of ease in passing on parameters of enum class type can backward compatible old concept of enum in c language collection of constants? reason?

what talking (typeandsize.species.empty) not fellow member field of spicies. when talk "member field", implies instance variable (for possible write in enum in java).

in aspect asking, can interpret enum beingness special shorthand of writing special class class constants:

enum foo { a, b; }

is similar

class foo { public static final foo = new foo(); public static final foo b = new foo(); private foo() {} }

(there still lot of difference between enum , handcrafted class, not yet concern)

java enums

No comments:

Post a Comment