Levy Schiavetti Levy Schiavetti - 1 year ago 79
Java Question

Method with restrict parameter

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

Toast.makeText
other than
Toast.LENGTH_LONG
or
Toast.LENGTH_SHORT
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 {
    LONG,
    SHORT
}

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