Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Enums

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I was going through enums ...

Prior to Java 5.0, you had to use static final int constants to create a list of enumerated variables as following example.

public static final int GENDER_MALE = 0;
public static final in GENDER_FEMALE = 1;

Although the above code solves the problem but this pattern of creating enumerated type has some problems such as the above variables are not typesafe since both variables GENDER_MALE and GENDER_FEMALE are just int variables so you can pass in any other int value where one of the above constant gender value is required.


could you please explain what is type safe implies here ...does it mean that we can assign some other integer value such as for ex 45, where only 0 and 1 is required...!!please advise..!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to compile the following code and see what happens:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:
could you please explain what is type safe implies here ...does it mean that we can assign some other integer value such as for ex 45, where only 0 and 1 is required...!!please advise..!!


That's the exact opposite of type-safe. So, yes, the problem they're talking about is that when we use ints like in the "bad old days", we can assign any old int value, regardless of whether it matches one of our named values. Thus, it is not type-safe. The int type was not a good mapping for the enum types.

Now, as Bear's code demonstrates, the Java language includes type-safe enums. That is, we define exactly which values each of our enum types can have, and it's not possible to assign an invalid value. If we try, our code won't compile.

(Before Java added enums in 1.5, people would roll their own type-safe enums. It was ugly.)
 
Winston Gutkowski
Bartender
Pie
Posts: 10073
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:Prior to Java 5.0, you had to use static final int constants to create a list of enumerated variables as following example.

public static final int GENDER_MALE = 0;
public static final in GENDER_FEMALE = 1;

Incorrect, you could have coded:
public static final Object GENDER_MALE = newObject();
public static final Object GENDER_FEMALE = new Object();

which would accomplish exactly the same thing, with the added bonus that it can't be abused (although it is not type-safe).

Although the above code solves the problem but this pattern of creating enumerated type has some problems such as the above variables are not typesafe since both variables GENDER_MALE and GENDER_FEMALE are just int variables so you can pass in any other int value where one of the above constant gender value is required.

And more importantly, you can pass a "Gender" value anywhere an int is required; and that's what makes it not type-safe.

Furthermore, why MALE == 0 and FEMALE == 1? These days, women might be happy because they're valued higher than men; but it also means that they'll be sorted in 2nd place, and so "walking behind the man". Not PC.

could you please explain what is type safe implies here

The fact is that, for anything but trivial stuff, ints just don't cut the mustard. A "Gender" is something very specific, and should only be used for code that requires a Gender type and that's where the type-safety comes in.

Before Java 1.5, it was possible to create type-safe constants, and they looked a bit like Singletons; viz:but they relied on programmers knowing the pattern (which many didn't); so the design bods added the enum (and not before time). Enums also allow you to do a bit more than the old "type-safe constant" pattern did (look up the Enum class or read the JLS for more details).

...does it mean that we can assign some other integer value such as for ex 45, where only 0 and 1 is required...

No. What it means is that the constant should NOT be an integer.

So (finally), getting back to your question of "what does type-safe mean?":
It means that your constant (or bunch of similar constants) is a type; and that means that it can't be used anywhere that it's not defined for.
Ergo, if you set up an enum called 'Gender', it can only be used by methods defined to take a Gender.

HIH

Winston
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
And more importantly, you can pass a "Gender" value anywhere an int is required; and that's what makes it not type-safe.


IMHO, it's the exact opposite. That is, the fact that you can pass any int where a "Gender" is required makes it not type-safe. Something that is expecting a "Gender" of 1 or 2 could get 3, 4, 5 or -42, and the compiler won't know anything is wrong with it.

So (finally), getting back to your question of "what does type-safe mean?":
It means that your constant (or bunch of similar constants) is a type; and that means that it can't be used anywhere that it's not defined for.
Ergo, if you set up an enum called 'Gender', it can only be used by methods defined to take a Gender.


And, just as importantly (more importantly if you ask me ), it means that arbitrary ints (or whatever other type we may have been using to simulate our type) can't be used where our type is called for.
 
Winston Gutkowski
Bartender
Pie
Posts: 10073
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:And, just as importantly (more importantly if you ask me ), it means that arbitrary ints (or whatever other type we may have been using to simulate our type) can't be used where our type is called for.

Yes, but, as I pointed out in my post, you can achieve that with a simple Object; but it doesn't make it type-safe.

Fraid we'll have to disagree on this one.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic