# How is Enum interpreted here?

• 06-13-2013, 08:26 PM
PaulAsbome
How is Enum interpreted here?
Hi, recently I have come across this class diagram regarding numbers and there's this part about the numbers class that I don't understand at all which I would like insight from.

http://i40.tinypic.com/2mpl9j4.png

As the picture above, how does the value val come into question in this case and how should I interpret it in a coding way?
• 06-14-2013, 06:39 PM
Fou-Lu
I've never seen an enum actually written out in a uml style before. If I had to guess at it, this is what it represents:
PHP Code:

``` public enum Number{    ONE(1),    TWO(2),    THREE(3);        private final int val;        private Number(int val)    {        this.val = val;    }        public int getVal()    {        return this.val;    }}  ```
The val comes to being the actual value associated with the enum number. It could be any datatype you want and can be called just like:
PHP Code:

``` Number one = Number.ONE;System.out.println(one.getVal());  ```
So if getVal() where to return a string, that can be done and replace the "one" value during the "construction" phase of the enum.
• 06-14-2013, 07:31 PM
PaulAsbome
Quote:

Originally Posted by Fou-Lu
I've never seen an enum actually written out in a uml style before. If I had to guess at it, this is what it represents:
PHP Code:

``` public enum Number {     ONE(1),     TWO(2),     THREE(3);          private final int val;          private Number(int val)     {         this.val = val;     }          public int getVal()     {         return this.val;     } }  ```
The val comes to being the actual value associated with the enum number. It could be any datatype you want and can be called just like:
PHP Code:

``` Number one = Number.ONE; System.out.println(one.getVal());  ```
So if getVal() where to return a string, that can be done and replace the "one" value during the "construction" phase of the enum.

Hi thanks for the reply, I appreciate it alot! :thumbsup:

I got a question though, in the 2nd example you gave, does that mean that the val of one.getVal would be 1, since it is associated with the enum of ONE?
• 06-14-2013, 09:13 PM
Fou-Lu
That's correct, that's defined here:
PHP Code:

```     ONE(1),  ```
In the enum block. It literally constructs an instance of itself, and allows you to operate on it since the Number enum datatype includes the getVal method.
You can add as many methods as you wish, much like any standard class object. The only real difference is that the enums are enumerated types, so it MUST be one of the options it provides, while the class instantiation doesn't have that limitation.
Also, unlike a class, enums cannot be instanated with the new keyword at all (the enum override forbids the use of public on a constructor scope). This also means that via reflection, it will fail to invoke on a call to .newInstance() since that would be defined as an InstantiationException.

One of the most useful features of the enum in java is that you can switch on an enum. This is especially important for people like me, who are used to being able to switch on strings (java does not allow you to switch on anything non-primitive EXCEPT for enum). It is slightly more bizarre though:
PHP Code:

``` Number n = Number.THREE;switch (n){    case THREE:        System.out.println("Its a three.");        break;}  ```
Note that its not cased on Number.THREE, rather just THREE as its capable of picking out the scope as being that of Number.
• 06-15-2013, 12:43 PM
PaulAsbome
Quote:

Originally Posted by Fou-Lu
Also, unlike a class, enums cannot be instanated with the new keyword at all (the enum override forbids the use of public on a constructor scope). This also means that via reflection, it will fail to invoke on a call to .newInstance() since that would be defined as an InstantiationException.

Thanks for your more detailed explanation about enum and I am actually awed by how powerful enum is on the programming aspect.However, what I don't understand is the part on Instantiation. What does that exactly mean? Does that mean that I am not allowed to do something like this?

Using the previous Number Enum as example:

PHP Code:

``` Number Number1 = new Number("1")  ```
• 09-03-2014, 07:58 AM
soniavdavidson
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum.
• 09-30-2014, 07:21 AM
Jahorshinma
Public class Person
{
[Key]
Public int PersonId { get; set: }
Public int Age { get; set; }

public int FavoriteColorValue{ get; set;}
[NotMapped]
Public ColorEnum FavoriteColor
{
get{ return (ColorEnum)FavoriteColorValue; }
set{ FavoriteColorValue = (int)value; }
}
}

It is one of the example for interpreting the enum

Public Enum ColorEnum
{
Red = 1,
Green = 2,
Blue = 3
}