Alrighty, so I did some research on constants and how they should be designed and used. For my application, it made sense to have numerous enums that would group terms that are related to each other.
The idea being that as I develop web services with hundreds of parameters (many of which are used more than once) and methods, I could annotate using the values of the enums. Before this, there was a huge, disgusting Constants file with redundant and unmaintained values.
So, here's an enum I'd like to use:
package com.company.ws.data.enums;
/** This {@link Enum} contains all web service methods that will be used. **/
public enum Methods {
/** The name of the web service for looking up an account by account number. **/
FIND_ACCOUNT_BY_ACCOUNT_NUMBER("accountByNumberRequest");
/** The String value of the web service method name to be used in SOAP **/
private String value;
private Methods(String value) {
this.value = value;
}
/**
* @return the String value of the web service method name to be used in
* SOAP
*/
public String getValue() {
return this.value;
}
}
And here's a place I'd like to use it:
package com.company.ws.data.models;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import com.company.ws.data.enums.Methods;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = **Methods.FIND_ACCOUNT_BY_ACCOUNT_NUMBER**, namespace = "com.company.ws")
public class AccountByNumberRequest{
}
So, if I try the above, I get the error Type mismatch: cannot convert from Methods to String
which makes perfect sense. So, let's try accessing the actual value of the enum:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = **Methods.FIND_ACCOUNT_BY_ACCOUNT_NUMBER.getValue()**, namespace = "")
public class AccountByNumberRequest extends RequestByAccount {
}
Doing that, I get this error message: The value for annotation attribute XmlRootElement.name must be a constant expression
.
So, can I use enums like I'm trying to? Can they be used in place of true static constant values as defined in a final class? Or am I in some weird compile-time state where the annotations are being evaluated before the enum itself is loaded and instantiated with its values? Guiding Resource: http://www.javapractices.com/topic/TopicAction.do?Id=1