Previous Topic Next topic Print topic


Enumeration - in COBOL and Java

Enumeration in Managed COBOL

enum-id Action public.
working-storage section. 
01 binary-long.         *> Underlying type for the enum. 
                        *> binary-long is the default
    78 Starter value 0. *> Declare enum values as level-78 items
    78 Stop    value 1.
    78 Rewind  value 2.
    78 Forward value 3.
end enum. 
*> Short hand version of above.
*> PUBLIC and BINARY-LONG are the defaults
*> The default values are 0 1 2...
enum-id Action.
   78 Starter.  
   78 Stop.
   78 Rewind.
   78 Forward.
end enum.
enum-id Status.
   78 Flunk value 50.
   78 Pass value 70.
   78 Excel value 90.
end enum.
program-id main.
   display type Status::Pass as binary-long *> prints 70
   display type Status::Pass                *> prints Pass
end program. 

Enumeration in Java

enum Action {Start, Stop, Rewind, Forward};
public enum Status 
{
    Flunk (50), Pass (70), Excel (90);
    private final int val;
    Status(int valIn)
    {
	  this.val = valIn;
    }
}

Action a = Action.Stop;
if (a != Action.Start)
  System.out.println(a);                // Prints "Stop"
	 
System.out.println(Status.Pass.val);    // Prints 70
System.out.println(Status.Pass);    	// prints Pass

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.

Previous Topic Next topic Print topic