jave 5.0 - enum can I assign values?

T

timasmith

per
http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/

there was an example of implementing enums prior to 5.0 (see below).

In 5.0 you have enum which is sweet for assigning arbitraty values but
I want to assign specific values and gain all of the other benefits of
using an enum...

so I want (can I have...?)

enum ASC {abc=3,ced=4};

and if you look at the cited pre 1.5 example you have that same
flexibility

public class MainMenu {
private final String name;

private MainMenu(String name) {
this.name = name;
}

public static final MainMenu FILE = new MainMenu("file");
public static final MainMenu EDIT = new MainMenu("edit");
public static final MainMenu FORMAT = new MainMenu("format");
public static final MainMenu VIEW = new MainMenu("view");

public String toString() {
return name;
}
}
 
R

Roland de Ruiter

per
http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/

there was an example of implementing enums prior to 5.0 (see below).

In 5.0 you have enum which is sweet for assigning arbitraty values but
I want to assign specific values and gain all of the other benefits of
using an enum...

so I want (can I have...?)

enum ASC {abc=3,ced=4};

and if you look at the cited pre 1.5 example you have that same
flexibility
[...]

public enum ASC {
abc(3), ced(4);

private final int foo;

private ASC(int bar) {
this.foo = bar;
}

public int getFoo() {
return foo;
}

public static void main(String[] args) {
for (ASC asc : ASC.values()) {
System.out.println(asc + ".getFoo()=" + asc.getFoo());
}
}
}
<http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top