Reputation: 121
I have been reading this book on java about runtime type information and I am very confused about class objects and the class Class. The question maybe silly but I am kind of confused. When we normally write a class we use smaller case c for the word class. eg. public class Foo with the class name starting with upper case. so the class Class is upper case C for the class name. Now the confusion is about the class object. which is also upper case for that class. so now forexample if we have say a method in a class such as
public void countClass(Class<?> type) {
Class<?> superClass = type.getSuperclass();
}
public TypeCounter (Class<?> baseType) {
this.baseType = baseType;
}
Now in the first method what I understand is that the method countClass of type void has an argument of any type of class object? The Class referring to here is what exactly?? The class CLass? or the Class object? if it is a class object of any type why cant we just write the name of the superclass here instead of complicating things so much?
In the second method there is no type stated at all for the method TypeCounter? how does that work? and again it has a type of Class in the argument passed?
If these are referring to the Class class then what does it really mean? and if it is just the Class object then what does the class object really do in this case here?
There are classes like this:
static class PetCounter extends LinkedHasMap<Class<? extends pet>,Integer> {
....}
now the Class is what really? class Class or Class object? I'm sorry if I am confusing you with this question. But please try to answer.
Thanks in advance.
Upvotes: 3
Views: 319
Reputation: 2471
Let's have a look at the javadoc for the Class class:
Instances of the class Class represent classes and interfaces in a running Java application. ...
Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
...
It is also possible to get the Class object for a named type (or for void) using a class literal. See Section 15.8.2 of The Java™ Language Specification. For example:
System.out.println("The name of class Foo is: "+Foo.class.getName());
The keyword class
is used for a class declaration (define a class) or as a class literal (express the Class object of a class for runtime use)
public class Foo {}
Class<Foo> fooClass = Foo.class;
The Class
class explained above is used at runtime to represent the class of an object as an object.
Methods[] methods = fooClass.getMethods();
Foo foo = fooClass.newInstance();
Another example, getting the Class
from an instance of an object:
Foo foo2 = new Foo();
Class<Foo> fooClass2 = foo2.getClass();
Upvotes: 1
Reputation: 11
First,you have to know the difference between "class" and "Class".We put similar things into the same category,i.e."class".For example,car and bicycle can be in the vehicle class.Certainly,all classes can be put into the same category,i.e."Class"."Class" means the archetype of "class".Every object of a class has its own Class which reserves its information like name,properties,methods and so on.
Upvotes: 1
Reputation: 71
Class type variables in java holds the metadata (like methods details, annotations , implemented interfaces by class etc..) of the classes. It is not associated to any particular instance of class but is same for all instances of a given class.
Whereas Lower case class is a reserved keyword for class declaration.
In first method argument is of type Class, and it is not an instance but a metadata holder created from
Class<?> metadata = AnyClass.class;
This probably won't answer your entire question but hopefully give you a direction to think upon.
Upvotes: 1
Reputation: 4872
type
is a reference to a class of unknown type (hence <?>
).
Lets say you have a Class Car
you would get the runtime representation of this car passed to countClass. Then with getSuperClass you would get the super class of Car
. This may be Object or something else.
The second one is a constructor which has one Parameter of type Class
for a unknown type (hence the <?>
).
You last example is a static inner class with the Name PetCounter
which derives from a LinkedHashSet which in turn only accepts objects of type Class which are instantiated of pet. (I guess pet
should be upper case).
What this means is this: You can only add objects of type Class which represent some type of pets.
You have to look at it this way: In java everything is a object except primitives (int, boolean etc). This means, if I call myObjectInstance.getClass()
I get an object representing the metainformation of this object. This object is of type Class
. Since I have multiple classes in my java project, E.g. Car
Vehilce
etc. I can further specify what this meta information will contain hence I can specify the type of this classes with a generic. If I don't know this, I can specify <?>
.
Upvotes: 2
Reputation: 3409
The <?>
is the so-called wildcard argument meaning, that it can be an object of any type, without specifying the superclass. We could narrow it down, by defining a superclass (<? extends A>
), or a subclass (<? super B>
), but in this case we don't.
And the second method is actually a constructor, which is kind of a class boilerplate, used to pass values to initialize class fields. It's a fundamental OOP concept, be sure to read more about it.
Upvotes: 1