Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Confused about enums a little

Thread view: 
aaronfude@gmail.com - 10 Feb 2006 21:19 GMT
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
Eric Sosman - 10 Feb 2006 21:49 GMT
aaronfude@gmail.com wrote On 02/10/06 16:19,:
> Enums and type safety are obviously a great idea, but in one instance
> there seems to be more typing or am I wrong?
[quoted text clipped - 17 lines]
> 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.)

Signature

Eric.Sosman@sun.com

Douwe - 10 Feb 2006 22:08 GMT
@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 ????
Roedy Green - 10 Feb 2006 22:40 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Daniel Dyer - 10 Feb 2006 22:12 GMT
> aaronfude@gmail.com wrote On 02/10/06 16:19,:
>> Enums and type safety are obviously a great idea, but in one instance
[quoted text clipped - 23 lines]
> 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.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Roedy Green - 10 Feb 2006 22:01 GMT
>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;
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 10 Feb 2006 22:52 GMT
> Enums and type safety are obviously a great idea, but in one instance
> there seems to be more typing or am I wrong?
[quoted text clipped - 17 lines]
> 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
Thomas Hawtin - 11 Feb 2006 10:56 GMT
> Enums and type safety are obviously a great idea, but in one instance
> there seems to be more typing or am I wrong?
[quoted text clipped - 14 lines]
>
> 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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

P.Hill - 14 Feb 2006 05:04 GMT
> Enums and type safety are obviously a great idea, but in one instance
> there seems to be more typing or am I wrong?
[quoted text clipped - 10 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.