0

I have this class constructor AppData(Map params, Operations operation)
where Operations is

public enum Operations 
{
create,update,delete,view,compare
}

How can I instantiate AppData with parameters constructor?

for the map parameter its fine i get that from servlet, (req.getParameterMap())
but the main problem for me is knowing the operation type, which also comes from the map parameter, So in order to do this AppData data=new AppData(req.getParameterMap(),op); what shall I assign to op or what shall an op type be?

Note: I can get the String create , update ... from req.getParameterMap() by using iterator and Map.Entry object.getKey() and Value

2
  • duplicate of stackoverflow.com/questions/604424/… ? Commented May 18, 2011 at 12:54
  • 1
    enums should be given names with capital letters: CREATE instead of create and so on. I suggest a refactoring to implement the Java naming conventions (increases readability). Commented May 18, 2011 at 13:01

1 Answer 1

3

Assuming you're starting with a string (e.g., "update"), you can use Enum.valueOf. Your Operations enum will inherit a form of that, so:

Operations op = Operations.valueOf(theString);

...or use the one from Enum, though you'd really only have to do this if you don't have direct, compile-time access to Operations. I think you do have compile-time access, so use the above. But for completeness:

Object op = Enum.valueOf(Class.forName("Operations"), theString);
Sign up to request clarification or add additional context in comments.

2 Comments

is it, Enum.valueOf("update") ? Also, I need to store "update" in some var first.
@Abhishek: I've shown both the usual forms. If you have compile-time access to Operations, use Operations.valueOf(theString); If not, use Enum.valueOf and pass in the Class object of the enum and the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.