
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to iterate the values of an enum using a for loop in Java?
Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −
enum Days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
You can retrieve the contents of an enum using the values() method. This method returns an array containing all the values. Once you obtain the array you can iterate it using the for loop.
Example
public class IterateEnum{ public static void main(String args[]) { Days days[] = Days.values(); System.out.println("Contents of the enum are: "); //Iterating enum using the for loop for(Days day: days) { System.out.println(day); } } }
Output
Contents of the enum are: SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY
Example
enum Vehicles { //Declaring the constants of the enum ACTIVA125, ACTIVA5G, ACCESS125, VESPA, TVSJUPITER; int i; //Instance variable Vehicles() { //constructor } public void enumMethod() { //method System.out.println("Current value: "+Vehicles.this); } } public class Sam{ public static void main(String args[]) { Vehicles vehicles[] = Vehicles.values(); for(Vehicles veh: vehicles) { System.out.println(veh); } vehicles[3].enumMethod(); } }
Output
ACTIVA125 ACTIVA5G ACCESS125 VESPA TVSJUPITER Current value: VESPA
- Related Articles
- How to iterate the values in an enum in Java?
- How to iterate a Java List using For Loop?
- How to iterate a List using for Loop in Java?
- How to iterate a Java List using For-Each Loop?
- How to iterate a List using for-Each Loop in Java?
- How to loop through all values of an enum in C#?
- Java Program to Iterate over enum
- How to iterate for loop in reverse order in Swift?
- How to call another enum value in an enum's constructor using java?
- What are enumerations in Java? How to retrieve values from an enum?
- How to convert a JSON object to an enum using Jackson in Java?
- Iterate through the values of Java LinkedHashMap in Java
- How to iterate the contents of a collection using forEach() in Java?
- Iterate through the values of HashMap in Java
- Iterate through the values of TreeMap in Java

Advertisements