Computer Science 1: Enumerations

Lesson 17

This is a simple one. Java will let you define an enumerated type- or limit all the possible values a variable can have. For example:

enum Entrees {chicken, beef, fish, pork, tofu}

Entrees dinnerMenu;

dinnerMenu = Entrees.beef;

The variable dinnerMenu is now restricted to the 5 options given by the type Entrees. It is considered type-safe, which means attempted use of a value that doesn’t exist will result in a compile-time error. The value is accessed using dot notation.

Published by Bethany Hull

Currently working on a BS in Computer Science from Boise State. BA in Dance from BYU-I. Yoga teacher. Choreographer. Backpacker. Foodie.

Leave a comment

Design a site like this with WordPress.com
Get started