-4

I need a custom method to check for a list containing an instance of a class and call this method but I do not understand this syntax "Class clazz" and I do not understand What's the second parameter of this method

public static <E> boolean containsInstanceOfOidInrArraylist(List<E> Arraylist, Class<? extends E> clazz) {
        for (E e : Arraylist) {
            if (clazz.isInstance(e)) {
                return true;
            }
        }
        return false;
    }
m_davari
  • 3
  • 4
  • 3
    I recommend you to start by learning about `Generics`. – Aniket Sahrawat Mar 31 '19 at 15:00
  • 2
    The [Java Tutorial on Generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html) is probably a good place to start – D.B. Mar 31 '19 at 15:25
  • Possible duplicate of [Difference between super T> and extends T> in Java](https://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java). This has a nice explanation. – LppEdd Mar 31 '19 at 15:26
  • After reading the tutorial as recommended by DB, `Class` is the name of a class (it is `java.lang.Class`), and `clazz` is a variable name ... chosen because you can'e use `class` as a variable name. – Stephen C Mar 31 '19 at 15:34
  • Further, the combination of ```List``` and ```Class extends E>``` is telling you something about argument compatibility. Given```List``` (say) then```Class``` is not acceptable; but "Class" and "Class" are. –  Mar 31 '19 at 15:40
  • And finally, you can get a value for the second argument by an expression such as ```Integer.class`` -- i.e., every class has an associated Class instance/ –  Mar 31 '19 at 15:43

2 Answers2

0

Ok, so the first parameter of the function is a List<E> named Arraylist (You shouldn't capitalize variables in Java, name it as such arrayList).

The second parameter is a Class<? extends E> named clazz.

Check manub's explaination of Class<?>:

Class is a parameterizable class, hence you can use the syntax Class<T> where T is a type. By writing Class<?>, you're declaring a Class object which can be of any type (? is a wildcard). The Class type is a type that contains meta-information about a class.

So now you know what Class<?> means, but what about Class<? extends E>?

<? extends E> basically means any class which extends E (or E itself).

So Class<? extends E> clazz means you have a varaible named clazz which is E class or a sub class of E.

ElayM
  • 66
  • 1
  • 7
0

See this tutorial: https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html

"Upper Bounded Wildcards
You can use an upper bounded wildcard to relax the restrictions on a variable. For example, say you want to write a method that works on List<Integer>, List<Double>, and List<Number>; you can achieve this by using an upper bounded wildcard.

To declare an upper-bounded wildcard, use the wildcard character ('?'), followed by the extends keyword, followed by its upper bound. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces)."

The second parameter is a class that is the same as the class of the objects in the list, or extends it. clazz is used as a variable name because class is a reserved keyword and cannot be used.

  • Hi, please provide us your code and question. Now we don't know what is your problem with linked tutorial. – dgebert Mar 31 '19 at 16:11