Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

What is the use of enum in java? and how can it be used?

Im trying to understand how to use enum in java and finding it diffcult too, I'm not sure if it is on treehouse, could someone please help me understand it and how to use it,

Thank you

1 Answer

Hi Jiten,

Enumerated types are predefined constants that don't change. Some examples include: days of the week, directions on a compass, or colors in a rainbow.

The following example is an enumerated type containing days of the week:

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

The following example is an enum containing directions on a compass:

public enum Compass {
    NORTH, SOUTH, EAST, WEST
}

The following example is an enumerated type containing colors of a rainbow:

public enum Rainbow {
    RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET
}

Hope this helps :)

Hi Michael, Thank you, but how will can i use them ? and how are they used?

thank you