Confused about enums a little

A

aaronfude

Enums and type safety are obviously a great idea, but in one instance
there seems to be more typing or am I wrong?

class Pet {
enum Type { DOG, CAT, SNAKE };
String name;
public Pet(Type type, String name) {
//
}
}

Then when calling:

Pet pet = new Pet(Pet.Type.DOG, "Sparky");

With the old ugly final ints it was

Pet pet = new Pet(Pet.DOG, "Sparky");

Am I wrong or is it in fact more typing when refurring to the enums
from outside the class?

Thanks!

Aaron
 
E

Eric Sosman

Enums and type safety are obviously a great idea, but in one instance
there seems to be more typing or am I wrong?

class Pet {
enum Type { DOG, CAT, SNAKE };
String name;
public Pet(Type type, String name) {
//
}
}

Then when calling:

Pet pet = new Pet(Pet.Type.DOG, "Sparky");

With the old ugly final ints it was

Pet pet = new Pet(Pet.DOG, "Sparky");

Am I wrong or is it in fact more typing when refurring to the enums
from outside the class?

Yes, it's a little more typing. On the other
hand, it creates namespaces. Thus, you avoid
conflicts between names that belong to different
enums but happen to look the same. Paraphrasing
Bloch's "Effective Java:"

enum Orange { NAVEL, VALENCIA, BLOOD };
enum Fluid { BLOOD, SWEAT, TEARS };

(Sorry to hear that your pet has the mange and
needs to be refurred.)
 
R

Roedy Green

Pet pet = new Pet(Pet.Type.DOG, "Sparky");

You are not allowed to do that. The types are nailed down at compile
time. The constructors are private. If you want new Pet types you
have to add them to the Pet class.

You are only allowed to write

Pet pet = Pet.DOG;
 
D

Douwe

@Eric Sosman.

the thing with avoiding conflicts between names confuses me a bit. It
creates namespaces !??? .. you will always have to type Orange.BLOOD or
Fluid.BLOOD. You can not just type BLOOD.

old-style:

class Orange {
public static final int NAVEL = 0;
public static final int VALENCIA = 1;
public static final int BLOOD = 2;
}

class Fluid {
public static final int BLOOD = 0;
public static final int SWEAT = 1;
public static final int TEARS = 2;
}

so tell me how to get things messed up ????
 
D

Daniel Dyer

Yes, it's a little more typing. On the other
hand, it creates namespaces. Thus, you avoid
conflicts between names that belong to different
enums but happen to look the same.

It also prevents you from doing something silly, like this:

Pet pet1 = new Pet(Pet.DOG, "Sparky");
Pet pet2 = new Pet(Pet.CAT, "Elvis");
Pet pet3 = new Pet(Pet.SNAKE, "Gary");
Pet pet4 = new Pet(Integer.MIN_VALUE, "Some unspecified creature");


Dan.
 
R

Roedy Green

the thing with avoiding conflicts between names confuses me a bit. It
creates namespaces !??? .. you will always have to type Orange.BLOOD or
Fluid.BLOOD. You can not just type BLOOD.

In the new style, the context is obvious in a CASE label. so you can
leave off the Orange. You need it for pretty well every thing else,
even an Orange parameter.
 
O

Oliver Wong

Enums and type safety are obviously a great idea, but in one instance
there seems to be more typing or am I wrong?

class Pet {
enum Type { DOG, CAT, SNAKE };
String name;
public Pet(Type type, String name) {
//
}
}

Then when calling:

Pet pet = new Pet(Pet.Type.DOG, "Sparky");

With the old ugly final ints it was

Pet pet = new Pet(Pet.DOG, "Sparky");

Am I wrong or is it in fact more typing when refurring to the enums
from outside the class?

I don't think the goal of enums was to save typing, so it shouldn't be
surprising that you need more typing when using enums (nor should it be
surprising that you need less typing for that matter; it's somewhat
irrelevant to the design goals).

- Oliver
 
T

Thomas Hawtin

Enums and type safety are obviously a great idea, but in one instance
there seems to be more typing or am I wrong?

class Pet {
enum Type { DOG, CAT, SNAKE };
String name;
public Pet(Type type, String name) {
//
}
}

Then when calling:

Pet pet = new Pet(Pet.Type.DOG, "Sparky");

With the old ugly final ints it was

Pet pet = new Pet(Pet.DOG, "Sparky");

import static xyz.Pet.Type.*;

Pet pet = new Pet(DOG, "Sparky");

Or even:

enum PetType {
DOG, CAT, SNAKE;
public Pet create(String name) {
return new Pet(this, name);
}
}

import static xyz.PetType.*;

Pet pet = DOG.create("Sparky");

pet.dispose();

Tom Hawtin
 
P

P.Hill

Enums and type safety are obviously a great idea, but in one instance
there seems to be more typing or am I wrong?

class Pet {
enum Type { DOG, CAT, SNAKE };
String name;
public Pet(Type type, String name) {
//
}
}

Then when calling:

Pet pet = new Pet(Pet.Type.DOG, "Sparky");

With the new syntax you can go several ways:
1. Type does NOT have to be nested in Pet, so
all could be in the same vet package so
that a simple

import vet.*;

would allow references to Pet and references to
Species (assuming it's better not to have an
overly generic name like Type running around).

You can then also use the new
import static vet.Species;
or if you love the nested-class
import static vet.Pet.Species;

and then you CAN reference
DOG, CAT and SNAKE with less syntax then
before.

I managed to get the following line to compile:

Pet myPet = new Pet(DOG, "Sparky");

With
a. a class Species
b. a class Pet
c. a static import

It is the static import which gives you shortened syntax.

import static; It's not just for java.lang.Math!

-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top