Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Enum with annotations

807580Aug 16 2010 — edited Aug 16 2010
Hi,

I using the enum with annotations, for that i have following classes :-

1) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface intValue {
int numericValue();
}

2) public enum Numbers {
@intValue(numericValue = 1)
ONE,
@intValue(numericValue = 2)
TWO,
@intValue(numericValue = 3)
THREE;

int format;

private Numbers(){

}

private Numbers(int format) {
this.format = format;
}

public int getValue() {
return this.format;
}

}

3) public class AnnotationEnumTest {

public static void main(String args[]){
try{
String field = Numbers.ONE.toString();
System.out.println("the final value is:::"+Numbers.THREE.getValue());
}catch(Exception e){
e.printStackTrace();
}

}

Here in the Main class i need to get the nuericValue "3" as supposed to "THREE", hence i tried with Numbers.THREE.getValue() which is returning "0". If i use Numbers.THREE.toString() i will get the "THREE" which i dont need . I need to get the value "3"? How do i get it?

Thanks in advance.


}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 13 2010
Added on Aug 16 2010
8 comments
454 views