| Author |
enum details required? using properties
|
Shanmugam nagaraj
Ranch Hand
Joined: May 07, 2007
Posts: 76
|
|
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?
|
Thanks <br />Shanmugam N
|
 |
Vijayender Bandaru
Greenhorn
Joined: Nov 21, 2006
Posts: 21
|
|
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
Joined: Jun 04, 2007
Posts: 318
|
|
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
|
------------------------
Bob
SCJP - 86% - June 11, 2009
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
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?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: enum details required? using properties
|
|
|