简体   繁体   中英

Understanding Class<?>

I came across this code:

public class RestfulAdage extends Application {
  @Override
  public Set<Class<?>> getClasses() {
    Set<Class<?>> set = new HashSet<Class<?>>();
    set.add(Adages.class);
    return set;
  }
}

I do not understand what Class<?> means.

Class<?> refers to a class of unknown type. The notation uses an unbounded generic which places no restriction on the type of class that can be added to the Collection. For example the following would not work

Set<Class<String>> set = new HashSet<Class<String>>();
set.add(Adages.class); // type not allowed

Class is a parametrizable class, hence you can use the syntax Class 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 metainformation about a class.

It's always good practice to refer to a generic type by specifying his specific type, by using Class you're respecting this practice (you're aware of Class to be parametrizable) but you're not restricting your parameter to have a specific type.

Reference about Generics and Wildcards: http://docs.oracle.com/javase/tutorial/java/generics/wildcards.html

Reference about Class object and reflection the (feature of Java language used to introspect itself): http://java.sun.com/developer/technicalArticles/ALT/Reflection/

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

The following sections discuss wildcards in more detail, including upper bounded wildcards, lower bounded wildcards, and wildcard capture.

for more information click here

It refers to gererics. I suggest you read a little on it. Basically, you know only at runtime what type of object you get to work with. For example, Class can be Integer, String or even YourDefinedClassType
read here http://java.sun.com/developer/technicalArticles/J2SE/generics/

From : Wildcards

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

Check the link, you will find more exhaustive documentation, examples etc.

Assume that you have a set of classes that belong to different types , and you have instances of different classes as well. So if you want to check whether these instances are instanceof one of these classes, you could iterate through these set and do the job. And for that kind of job, you better use a totally unrestricted set:

public boolean checkClasses(Set<Class<?>> typeLessClassSet, Set instances){

    while(typeLessClassSet.hasNext()){
        Class c = typeLessClassSet.next();
        while(instances.hasNext()){
            Object o = instances.next();
            if(o instanceof c)
            return true;
        }
    }
        return false;    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM