• Post Reply Bookmark Topic Watch Topic
  • New Topic

Anybody know how to use "enum" in Java?  RSS feed

 
Jon Campbell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code compiles, but I don't have any documentation on how to use the "enum" structure. I cant find any info on it in my books or Sun's language documentation...

class Test {

enum eone {
Plus { int operate(int x) { return ++x;} },
Minus { int operate(int x) { return --x;} }
}


public static void main (String[] args){
System.out.println("eone="+eone.Plus);
}

}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#enums
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make the enum really useful, it's missing an abstract method declaration:



You can use that enum like a normal class, with Plus and Minus being final static fields of the type eone (in fact that's what it *is*).
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read Sun's Enum Tutorial?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!