Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class object and class Class

Tags:

java

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.

like image 962
Pankaj Lohia Avatar asked Jul 08 '26 00:07

Pankaj Lohia


1 Answers

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 <?>.

like image 185
morpheus05 Avatar answered Jul 14 '26 07:07

morpheus05



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!