Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trafficLights;
- public enum TrafficLight {
- //Enums should be in Uppercase letters
- RED ("Stop"),
- YELLOW ("Wait"),
- GREEN ("Go");
- private String action;
- //Enum constructor can only be private or default
- private TrafficLight(String action) {
- this.action = action;
- }
- //getter
- public String getAction() {
- return action;
- }
- }
- =================================================================================================================================
- package trafficLights;
- public class TrafficLightTester {
- public static void main(String[] args) {
- TrafficLight[] lights = TrafficLight.values();
- //for-each loop access Enum elements
- for (TrafficLight light : lights) {
- System.out.println("Light: " +light.name() + " --- " +
- "Action: " + light.getAction());
- }
- //use light.toString() can also give the same output
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement