Implementing the C enum Keyword

Implementing the C enum Keyword

I’m porting my C code to Java and would like to know how I would implement the C enum keyword to Java.

There is no convenient mechanism for enum in Java. You just list the constants one by one, and you must attach values to them. You also must find a “home” for them, some class to which the constants conceptually belong.

For example,

        enum Weekday { MON, TUE, WED, THU, FRI, SAT, SUN }; // C        

might turn into

       class Day // Java       {       public static int MON = 0;               public static int TUE = 1;               public static int WED = 2;               public static int THU = 3;               public static int FRI = 4;               public static int SAT = 5;               public static int SUN = 6;               // . . .       }        

Note that this does not introduce a special type. A variable that canhold weekdays must be defined as an int.

Contrast that with C, where you can define a variable of type Weekday:

        enum Weekday w;        

Actually, in C, this is just window-dressing. An enumeration is just aninteger in C. But in C++, enumerated types really are distinct types.

On the positive side, names are inside the scope of that class. Forexample, Sunday is

        int w = Day.SUN; // Java        

(This is completely analogous to enumerations defined inside classes inC++.) This is a good thing because it saves you from embarrassingmultiple definitions of enumeration constants, like

        enum Test { GRE, GMAT, SAT };        enum Vendor { DEC, HP, SUN };    
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes