Solved

C-like enum in Java?

Posted on 1997-03-14
2
226 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now