• Post Reply Bookmark Topic Watch Topic
  • New Topic

enum details required? using properties  RSS feed

 
Shanmugam nagaraj
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Any body explain me
1) what is enum?
2) why we using enum?
3) how to use it in step by step?
4) I need to access the properties file from the java which contains the enum can any body explain the program?
 
Vijayender Bandaru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer to these ..
1.J2SE enums
2. example

[ August 03, 2007: Message edited by: Vijayender R. Bandaru ]
[ August 03, 2007: Message edited by: Vijayender R. Bandaru ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a couple of simple statements that CERTAINLY dont tell the WHOLE story but they might help you develop an understanding....

enums give us a way to define a limited set of values to use for any purpose that we might have. When a specific enum is used ONLY one of the SPECIFIED VALUES can be put into that variable.

It is not and int, not a char, it is not any other type just being used to represent a value, rather it is an enum and it can only have the values that you specify.

An example might help..... let's look at a traffic light. Let's say that you want to model a typical traffic light at an intersection.....for simplicity's sake we will just address one side of it.

A traffic light typically has three colors, red, yellow, and green.

Certainly we could do this:

int red = 0;
int yellow = 1;
int green = 2;

int currentColor;

and to set current Color to any color we want, say read it becomes

currentColor = green;

and our job would be done. But let's look at a couple of things

a) how do you know, without doubt, that it was a red stored there? somebody could have stored their variable int number OfFeetPerChicken into currentColor.

b) when you go to test it for color....

if (currentColor == Dog.getNumberOfEyes()) will be true.....

as might if (currentColor == cardCount[].length) if there are two cards in the array at the time.

In other words, there is no safe way to KNOW for CERTAIN that a red, grren, or a blue is the ONLY thing in that currentColor.

----------------------------

another way

enum LightConditions {RED, YELLOW, GREEN}

you have now created your own class/type of data and given it three possible values.

now a variable

LightConditions myCurrentCondition;

I now have a variable that will accept only LightConditions.RED, LightConditions.YELLOW, or LightConditions.GREEN.
NOTHING ELSE.

You cannot stick a wild 1 or 7, or a '0' or anything else in that place.

And, when you go to test it

if (myCurrentCondition == LightConditions.YELLOW)

then the ONLY way that expression can be true is if myCurrentCondition has exactly LightConditions.YELLOW in it......... and nothing else.

Now, in the API you have other methods and capabilities....some of those will return values that refer to the relative order of the enums in the class but for most purposes, you might not ever need them. Fore most purposes you want something that can only have CERTAIN KNOWN VALUES and the ability to declare a variable that can ONLY hold those VALUES.

I hope that reading this helps out.

Bob
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shanmugam nagaraj:

4) I need to access the properties file from the java which contains the enum can any body explain the program?


I have no idea what that means. Can you please elaborate?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!