Tag Archives: enumerated type

Constant-specific class body (Java enum)

Consider the following example which defines an enum type with three constant values. The next–and admittedly somewhat contrived–example calls a constructor to associate an integer value with each enum constant. But how about if we wanted to override  getNum() for … Continue reading

Posted in Uncategorized | Tagged , , | 1 Comment

Public enum type with constructor, instance variable, and public method

This example program has two source files (Demo.java and Bucket.java) and demonstrates use of an enum type with a constructor, instance variable, and public method. Demonstration class:

Posted in Uncategorized | Tagged , , | Leave a comment

How to overload an enum constructor

The following example shows a Java enum type in its simplest form. To associate a Celsius value with each enum value, we could do this: The above example uses a constructor with a single argument, but the following example shows how we … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

Java enum types to represent card rank and suit

Here’s my solution to write a Card and Deck class using Java enumerated types (enum type) to represent the suit and rank of each card. Funnily enough, I had more problems remembering the names of the suits and ranks than … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

Java enum types and the values() method

The Java compiler adds special methods to an enum type when it is created, one example of which is the values method. This is a static method that returns an array of all values declared in an enum type. The … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment

Java programming: a simple example using the enum type

In Java, the enum declaration declares a class called an enum type. The following code shows a very simple enum type for days of the week. We cannot instantiate an enum type directly, but we can create variables of that … Continue reading

Posted in Uncategorized | Tagged , , | Leave a comment