Levy Schiavetti Levy Schiavetti - 2 years ago 101
Java Question

Method with restrict parameter

When you try to give another value to the 3rd argument in

other than
it won't accept.

I need to create a method and use the same type of restriction: it can only be accepted three values which I'll define previously using constants.

How can this be accomplished?

Answer Source

You can restrict the user by creating your own enum. Sample code:

public enum Notification {

Method which I want to restrict:

private void methodName(String value1, Notification value2){
// Write whatever you want to perform

Now, this method can only be called like:

methodName("", Notification.LONG); // Second Parameter is allowed only from your custom enum
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download