0

I have to work with an existing code, and I didnt find what this notation meant : Class<?>...

For instance, it is used in the method :

public static <T> HashSet<ConstraintViolation<T>> validate(Validator validator, T resource, Class<?>... groups) {
    return (HashSet<ConstraintViolation<T>>) validator.validate(resource, groups);
}

And called like this :

HashSet<ConstraintViolation<EntryDTO>> l = ValidationTools.validate(this.validator, entryDTO);

Does it mean that "groups" is optionnal ? I found this interesting topic : What does Class<?> mean in Java? but it doesnt really answer my question...

Thanks for your answers =)

Community
  • 1
  • 1
Logan Paul
  • 123
  • 1
  • 9

3 Answers3

3

It has nothing to do with Class<?>. The three dots in the end means that the final argument is a varargs argument

For more info, check the Java documentation and this StackOverflow answer.

Community
  • 1
  • 1
mavroprovato
  • 8,023
  • 5
  • 37
  • 52
1

The first answer you have linked to correctly tells you what Class<?> means. The ... is called varargs, and means 0 or more arguments. So the argument Class<?>... means any number of arguments of any type of Class.

For more info on varargs, see https://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

Pikaling
  • 8,282
  • 3
  • 27
  • 44
0

Class<?> means that this variable is any type of class object. The three dots means that you can pass any number of parameters in the method call.

So this are all valid calls to ValidationTools.validate method:

ValidationTools.validate(this.validator, entryDTO);
ValidationTools.validate(this.validator, entryDTO, myString.getClass());
ValidationTools.validate(this.validator, entryDTO, myString.getClass(), myInteger.getClass());

And so on with any number of parameters of type Class.

Paco Abato
  • 3,920
  • 4
  • 31
  • 54