Breako Breako
Breako Breako

Reputation: 6461

Making a parameter compulsory in Groovy

In groovy, it is possible to have a method as:

void myMethod(String param1, String param2, String param3, String param4)

Then invoke it with with param4 being null

myMethod(param1, param2, param3, null)

This means param4 is null. Is there any way to make param4 compulsory and non - null? So that you can never invoke it without specifying a value for it?

Upvotes: 2

Views: 120

Answers (1)

kdabir
kdabir

Reputation: 9868

You can always explicitly throw IllegalArgumentException when param4 is null or blank or whatever is your condition.

void myMethod(String param1, String param2, String param3, String param4) {
  if (!param4) throw new IllegalArgumentException("param4 is required blah blah!");
}

Or You can use @NotNull annotation.

Also Refer:

IllegalArgumentException or NullPointerException for a null parameter?

Which @NotNull Java annotation should I use?

Upvotes: 4

Related Questions