I want to write a method that only takes certain values for a parameter, like f.e. in the Toast
class in Android. You can only use Toast.LENGTH_SHORT
or Toast.LENGTH_LONG
as duration for the method makeText(Context context, int resId, int duration)
. I had a look at the source code of the Toast
class but found nothing there. How can I achieve that?

- 4,817
- 5
- 42
- 53
-
You want to show toast for a certain time other than the one provided by FrameWork? – Raghunandan Jul 12 '14 at 16:42
-
2you can use Enum http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – mromer Jul 12 '14 at 16:43
3 Answers
You can use @IntDef or @StringDef annotations for your methods like this:
@Retention(SOURCE)
@IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
public @interface NavigationMode {}
public static final int NAVIGATION_MODE_STANDARD = 0;
public static final int NAVIGATION_MODE_LIST = 1;
public static final int NAVIGATION_MODE_TABS = 2;
...
public abstract void setNavigationMode(@NavigationMode int mode);
@NavigationMode
public abstract int getNavigationMode();

- 1,410
- 14
- 25
-
5
-
@OmarElDon the question was asked for Android specifically so I provided answer for Android. You can see the tags of the question. – Marcin Bak Feb 09 '18 at 12:19
Use an Enum Type, from the Java Tutorial,
An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
As an example,
public enum MyEnum {
ONE, TWO;
}
public static void myMethod(MyEnum a) {
// a must be MyEnum.ONE or MyEnum.TWO (in this example)
}
Edit
To get String(s) from your enum types you can add field level values (which must be compile time constants) with something like,
public enum MyEnum {
ONE("uno"), TWO("dos");
MyEnum(String v) {
value = v;
}
private String value;
public String getValue() {
return value;
}
}

- 198,278
- 20
- 158
- 249
-
I want the parameters to be of a certain type, let's say string. How can I do that with Enums? Is that even possible? – kaolick Jul 12 '14 at 16:47
-
@kaolick You can give your Enum(s) String values, but they must be Enum types (which are actually constants). Let me edit to give you an example. – Elliott Frisch Jul 12 '14 at 16:48
-
-
@Raghunandan No. I want my method to only accept certain fix values for a parameter. – kaolick Jul 12 '14 at 16:51
-
@kaolick Then check the values to make sure they're allowable. You say "type", but you actually mean "value". You either need to use enums, types, or validate the values somehow. – Dave Newton Jul 12 '14 at 16:53
-
@kaolick You can define a `ToastType` (or `ToastLength`) enum similar to Elliott's example, but saving an `int` and constructing the two enumerations with `int` values `Toast.LENGTH_LONG` and `Toast.LENGTH_SHORT`. Now, have your methods use a `ToastType` argument instead of an `int` argument, and the methods can use `getValue()` to get the `int` when calling `makeText`. – ajb Jul 12 '14 at 16:56
-
@ajb I understand. But how does this work with int as parameter type in the `Toast` class? – kaolick Jul 12 '14 at 17:03
-
@kaolick you will have to have a Custom Toast class. If you want the features from the framework copy the same and modify the way you want. – Raghunandan Jul 12 '14 at 17:07
-
@Raghunandan It's not about the `Toast` class. It was just an example for a general problem. – kaolick Jul 12 '14 at 17:09
-
@kaolick You'd need a wrapper class or a wrapper method, i.e. your own `makeText` method that uses a `ToastType` argument and turns around and calls `Toast.makeText`. But if your aim is to prevent `Toast.makeText` from being called with illegal duration values, this will help accomplish that. – ajb Jul 12 '14 at 17:12
-
@ajb How can I do that? Can you give me an example? I want something like `myMethod(String myParam)` where I can use only certain values for myParam. I'd prefer that those would be final Strings in my class that contains `myMethod(String myParam)`. As I said, like in the `Toast` class. But maybe Google uses some other magic that we don't see here. ;-) – kaolick Jul 12 '14 at 17:21
-
2@kaolick If you're really set on using a `String` parameter, instead of defining another class (such as an `enum`) to represent the possible values, then this isn't going to work for you. Your alternative is to have the method check for valid inputs and throw an exception if they're not. – ajb Jul 12 '14 at 17:41
-
1@ajb You are right. But the solution using an Enum seems the better way because it cannot fail by throwing an Exception. Seems more safe though. – kaolick Jul 13 '14 at 08:55
To use ints as is done with the Toast class, you can do something like this:
public class MyClass {
//by convention, constant names are all caps
public static final int VALUE_ONE = 0;
public static final int VALUE_TWO = 1;
public void myMethod(int value) throws InvalidParameterException {
if(value != VALUE_ONE || value != VALUE_TWO) {
throw new InvalidParameterException();
//or set default value instead of throwing an exception
}
else {
//your code
}
}
}
VALUE_ONE
and VALUE_TWO
are static and final, meaning that throughout the entire application there will only be one instance of that variable, and its value will never change (if you know C, it is similar to a #DEFINE
). Thus, when someone passes MyClass.VALUE_ONE
as an argument, you know exactly what it is, every time, while the caller doesn't necessarily need to know the integer value behind the constant. And then you will want to do a runtime check to make sure that what was passed in was one of the valid values, and if not, throw an exception. Or, if the value passed in isn't very critical, you could just set a default value if the argument is incorrect rather than throw an exception.

- 1,368
- 1
- 13
- 25
-
I see. I hab something like that in mind but I was looking for a solution where I can avoid something like throwing an Exception. So I'm going with the Enum solution. But if someone wants to avoid using Enums, your solution seems correct. – kaolick Jul 13 '14 at 08:57