Solved

C-like enum in Java?

Posted on 1997-03-14
2
230 Views
Last Modified: 2012-08-13
Is there a C-like 'enum' in Java? Example:

C code:
enum tagType {
  TYPE_NULL = 0,
  TYPE_ONE,
  TYPE_TWO,
  ...
  TYPE_N;
} Type;

I've looked at many java sites and didn't see one.  Is there one? Or do I have to do this:

Java:
public interface tagType {
  final static int NULL = 0;
  final static int ONE = 1;
  final static int TWO = 2;
  ...
  final static int N = n;
}
0
Comment
Question by:igroove
2 Comments
 

Accepted Solution

by:
amarshal earned 20 total points
ID: 1218935
You've already found the answer.  You have to use a series of static variables.

Java's philosphy was based on simplicity.  So to prevent adding more structures to the language, enum and #defines were never included.

In the case of your NULL define, 'null' already exists as a Object pointer without an instance.
0
 

Expert Comment

by:pwyss
ID: 3536332
no you don't! there's a better soultion:

http://developer.java.sun.com/developer/Books/shiftintojava/page1.html

you get type-saftey and more...
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question