how to do "enum" in java?

Hi,

Is there any ways of doing 'enum' in java as in c or c++?

Thanks in advance.
denz_1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
http://www.javaworld.com/<b>javaworld</b>/javatips/jw-javatip27.html

Also a good discussion here:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20160731.html
Mick BarryJava DeveloperCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maheshexpCommented:
public interface Color{
       int red = 0;
       int blue = 1;
       int green = 2;
}
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

maheshexpCommented:
public interface Color{
       int red = 0;
       int blue = 1;
       int green = 2;
}

public class A implements Color{
    int x;

  public A(){
     x = red;  /* used the constant value here */
  }
}
lwinkenbCommented:
Here is another way to do enumerations:

Maybe you could do something like this:
public class Enum {
   public static final Obj APPLE = new Fruit();
   public static final Fruit ORANGE = new Fruit();
   public static final Fruit GRAPE = new Fruit();
   public static final Fruit MELON = new Fruit();

   private Fruit() {
   }
}

Then you could implement it with:
class Driver {
    public static void main(String[] args) {
         
         Fruit newFruit;
         newFruit = Fruit.APPLE;
         
         Fruit secondFruit;
         secondFruit = Fruit.ORANGE;
         
         if(newFruit == secondFruit)
              System.out.println("This wont work");
             
          secondFruit = Fruit.APPLE;
         
         if(newFruit == secondFruit)
              System.out.println("Aha!  There we go");
    }
}
lwinkenbCommented:
oops, change the first class to:

public class Fruit {
  public static final Fruit APPLE = new Fruit();
  public static final Fruit ORANGE = new Fruit();
  public static final Fruit GRAPE = new Fruit();
  public static final Fruit MELON = new Fruit();

  private Fruit() {
  }
}
Mick BarryJava DeveloperCommented:
Yes thats pretty much exactly what is suggested in the article I posted above :)
Mick BarryJava DeveloperCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.