Java Lesson 15: Enums

Advertisements

Hello readers,

It’s Michael, and today’s post will be about enums in Java. What exactly are enums?

Enums are special “classes” (that aren’t really classes in the Java sense) that represent groups of constants, such as unchangeable variables (which are any variables with the word final, as in public final int speed = 7). Remember that any final variables have set their values at the beginning of the class, rather than setting their values somewhere in the main code of the class. Also, you can’t change the values of final variables at any point in the main code.

  • Enums can be any list of strings or numbers, as you will see in the next example.
  • Enum is short for enumeration, which means “specifically listed”. It might be helpful to think of enums as lists of elements (but don’t confuse them with Arrays or ArrayLists)

So how do we create an enum? Take a look below:

public class Enums
{
enum Cats
{
Persian,
Bengal,
Sphinx,
Ragamuffin,
Calico
}
}

In this example, I created an enum that contains the names of five different cat breeds. To create an enum, remember to use the enum keyword instead of class, and remember to capitalize the first letter of each constant (if your enum contains words).

OK, the enum is great, but it can’t be run on it’s own. You’ll need a class with a main method, like this:

public class Enums
{
enum Cats
{
Persian,
Bengal,
Sphinx,
Ragamuffin,
Calico
}

public static void main(String[] args)
{
Cats myVar = Cats.Sphinx;
System.out.println(myVar);
}
}

run:
Sphinx
BUILD SUCCESSFUL (total time: 1 second)

In this example, I have incorporated the enum into the main class Enums (yes I know I could’ve picked a better name). In the main method, I am printing out an element from the enum.

But how did the main method return Sphinx as the output? Take a look at the line Cats myVar = Cats.Sphinx. This line tells Java to access the Sphinx element from Cats and store it in myVar (which is then printed out in the next line).

  • To access an element from an enum, remember to use the syntax Enum name.Enum element (and you should ideally store the accessed element in a variable of type Enum name (meaning the variable type should be the enum name).

You can also use an enum in a switch statement, like this:

public class Enums
{
enum Cats
{
Persian,
Bengal,
Sphinx,
Ragamuffin,
Calico
}

public static void main(String[] args)
{
Cats myVar = Cats.Calico;

switch(myVar)
{
case Persian:
System.out.println(“Persian cat”);
break;
case Bengal:
System.out.println(“Bengal cat”);
break;
case Sphinx:
System.out.println(“Sphinx cat”);
break;
case Ragamuffin:
System.out.println(“Ragamuffin cat”);
break;
case Calico:
System.out.println(“Calico cat”);
break;
}
}
}

run:
Calico cat
BUILD SUCCESSFUL (total time: 1 second)

In this example, I used a switch statement to display a certain output based on the value of myVar (the value of myVar was created using the Enum name.Enum element formula that I just discussed). Since the enum element I chose was Calico, the output displayed was Calico cat.

You can also loop through enums with a handy little method that is exclusive for enums. Here’s how to do so:

public class Enums
{
enum Cats
{
Persian,
Bengal,
Sphinx,
Ragamuffin,
Calico
}

public static void main(String[] args)
{
for (Cats myVar: Cats.values())
System.out.println(myVar);
}
}

run:
Persian
Bengal
Sphinx
Ragamuffin
Calico
BUILD SUCCESSFUL (total time: 2 seconds)

The handy little enum-exclusive method that I was talking about is the values method, which returns an array of all of the elements in the enum. The values method is the best way to loop through all of the elements in an enum-plus, I don’t think for loops, while loops, or do-while loops would work for iterating through enums.

  • The syntax for an enum loop looks quite similar to the syntax of a for-each loop.

So, you might still be wondering “What are the differences between an enum and a class?” Here are two significant differences:

  • Enums can have attributes and methods, just like classes can. However, the elements of enums are unchangeable and can’t be overridden at any point in the code.
  • Enums can’t create objects, nor can they extend other classes (so a line like enum extends [class name here] won’t work)

You might also be wondering when the best times to use enums would be. The answer to that would be that the best times to use enums would be when you have values that you know won’t change. Let’s say you’re building a poker program-the values of the 52 cards won’t change, so those card values could be your enum. Another example would be if you were building a calendar program-the months of the year could be an enum, since each year will always have the same 12 months.

Thanks for reading,

Michael

 

 

 

 

Leave a ReplyCancel reply