How can I use the value of a Java enumeration as a true static constant

Ok, so I did some research on constants and how they should be developed and used. For my application, it made sense to have a lot of enumerations that would combine terms related to each other.

The idea is that when developing web services with hundreds of parameters (many of which are used more than once) and methods, I could annotate the use of enumeration values. Before that, there was a huge, disgusting Constants file with redundant and unchanged values.

So here is an enumeration that I would 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 is the place I would like to use:

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{
}

, , Type mismatch: cannot convert from Methods to String, . , :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = **Methods.FIND_ACCOUNT_BY_ACCOUNT_NUMBER.getValue()**, namespace = "")
public class AccountByNumberRequest extends RequestByAccount {
}

, : The value for annotation attribute XmlRootElement.name must be a constant expression.

, , ? , ? - , , ? : http://www.javapractices.com/topic/TopicAction.do?Id=1

+4
2

, . Methods.getValue() JLS, .

+3

. , ( ) , API .

, , - , , -, : , , , , .

, - . (, , , , Java , , .)

+1

All Articles