I write:

Class<? extends Task> a = n.getClass(); 

The getClass() method should return an object describing the class and assign it to the variable а .

But this part is not clear for me. Class<? extends Task> Class<? extends Task> . What kind of variable is this and where is the variable now you can use it? I want to understand how it works. Help me to understand. especially < ? extends Task> question mark kills something here with ternary operation?

3 answers 3

Examples

  • Parameter type designation
 public static double sumOfList(List<? extends Number> list) { double s = 0.0; for (Number n : list) s += n.doubleValue(); return s; } public static void printList(List<?> list) { for (Object elem: list) System.out.print(elem + " "); System.out.println(); } 
  • Class field and return type
 public class ClassContainer { private List<? extends Number> list; public ClassContainer(List<? extends Number> list){ this.list = list; } public List<? extends Number> getList(){ return this.list; } } 

    No, what are you, there is no ternary operation here)) ? extends Object ? extends Object - means that there can be any class inherited from Object . Here is a similar entry:

     List<? extends Map<String, String>> 

      In the code where are used generic types a question mark ? called a wildcard search pattern and is of unknown type. This symbol is used in various situations:

      • indicates the type of parameter
      • fields of a class or type of local variable
      • return type.

      There are certain limitations to its use.

      • ABOUT! Thank! This is starting to shed light on the problem, but is it possible to add examples? Sorry for the arrogance ... how can it look in these 3 cases. - Pavel