Working with Enums Flashcards

1
Q

Defining a simple enum

public enum Season {
    WINTER, SPRING, SUMMER, FALL
}
A
  • public or package access
  • enum keyword
  • Enum name
  • Enum value (comma separated)
  • semicolon optional for simple enum
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Using an enum is super easy.

A

Use enum

var s = Season.SUMMER;
System.out.println(Season.SUMMER); // SUMMER
System.out.println(s == Season.SUMMER); // true

Call enum method
Season.SUMMER.printExpectedVisitors();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

enum rules

A
  • They can be compared using == because they are like static final constants, you can use equals() or == to compare enums
  • cannot extend an enum
    public enum ExtendedSeason extends Season {} // DOES NOT COMPILE
  • Although it is possible to create an enum with instance variables that can be modified, it is a very poor practice to do so since they are shared within the JVM. When designing an enum, the values should be immutable. ( should mark the instance variable private and final even it’s not required)
  • All enum constructors are implicitly private, with the modifier being optional. This is reasonable since you can’t extend an enum and the constructors can be called only within the enum itself. In fact, an enum constructor will not compile if it contains a public or protected modifier.
  • The first time we ask for any of the enum values, Java constructs all of the enum values. After that, Java just returns the already constructed enum values. Given that explanation, you can see why this calls the constructor only once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

enum methods
ex:
~~~
for(var season: Season.values()) {
System.out.println(season.name() + “ “ + season.ordinal());
}
~~~

A
  • enums print the name of the enum when toString() is called
  • An enum provides a values() method to get an array of all of the values.
  • Another useful feature is retrieving an enum value from a String using the valueOf() method.
    Season s = Season.valueOf("SUMMER"); // SUMMER
    Season t = Season.valueOf("summer"); // IllegalArgumentException
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Adding Constructors, Fields, and Methods

A
1: public enum Season {
2:     WINTER("Low"), SPRING("Medium"), SUMMER("High"), FALL("Medium");
3:     private final String expectedVisitors;
4:     private Season(String expectedVisitors) {
5:         this.expectedVisitors = expectedVisitors;
6:     }
7:     public void printExpectedVisitors() {
8:         System.out.println(expectedVisitors);
9:     } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

enum with abstract method

A
public enum Season {
WINTER {
public String getHours() { return "10am-3pm"; }
},
SPRING {
public String getHours() { return "9am-5pm"; }
},
SUMMER {
public String getHours() { return "9am-7pm"; }
},
FALL {
public String getHours() { return "9am-5pm"; }
};
public abstract String getHours();
}

This means that each and every enum value is required to implement this method. If we forget to implement the method for one of the values, we get a compiler error:

The enum constant WINTER must implement the abstract method getHours()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

enum with an default implementation for all values and override it only for the special cases.

A
public enum Season {
WINTER {
    public String getHours() { return "10am-3pm"; }
},
SUMMER {
    public String getHours() { return "9am-7pm"; }
},
SPRING, FALL;
    public String getHours() { return "9am-5pm"; }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

An enum can even implement an interface, as this just requires overriding the abstract methods:

public interface Weather { int getAverageTemperature(); }
public enum Season implements Weather {
WINTER, SPRING, SUMMER, FALL;
public int getAverageTemperature() { return 30; }
}
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly