3

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.

1
  • 1
    In all cases Class means the class called Class. Commented Aug 5, 2015 at 6:57

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

I thought that everything is an object. Even the class Class is an object. So when you say this object is if "type Class".. thats where i get confused. everything else is clear to me. What exactly do you mean by "type Class". When you say Class<?>.. the Class here is the Class object or the class Class? in "type Class", the Class you referring to is what?? the class Class or the object Class? or simply the object of class Class? . From what you have said, I can derive that... when you say (Class<Integer> foo) it simply means a reference name foo of type Intereger.
But again.. Why cant we just write Integer there? when you are stating Class<Integer> what are you implying? the Class Integer or Object type Integer?
The vm needs to create an object which represents the meta information for a specific object. Lets say I have a object of type Car and I want to know what fields and methods this object has. I can request this information with myCarInstance.getClass(). The VM represents this information as objects of type Class. The generic <?> or something is not relevant at all at this point.
Hey, I have one question here.. How did you know that the static class was actually an Innerclass?? I mean thats no where mentioned but you got it bang on.. you mind telling me how?. Also, I understood what you're saying here. Just that this Class type and Class Object is what is confusing. Basically all objects are of Class type.. because every object will fall in this "Class<?>" category. A foo object say foo.class will be equal to Class<Foo>?? And a Class object is the object of the Class class represented by that type? Thanks again
static keyword on classes in only allowed in inner classes.
|
1

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.

1 Comment

Hi, Sorry. I got confused. You are right about that being a constructor. however, I know about wildcard characters But The main point here is what exactly is this Class<?>? is it the class Class referring to any type of class or the class object referring to a class object of any type?? If it is a class object which I suppose it is, then what exactly is it trying to define here??
1

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.

2 Comments

Hey, thanks for your help. Thats what I was looking for. You have mentioned that the Class here is a "type". So is it then correct to say that the class here is referring to the class Class and not the Class object?
Yes, in the above method and constructor, it is correct to say that class is Class archetype and not its instance.
1

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.

1 Comment

there exactly is what I was looking for.. You understood my question well. Thanks. That was helpful
1

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();

3 Comments

this is the part that was confusing me..."The Class class explained above is used at runtime to represent the class of an object as an object ." Foo.class gives you an object of the Foo class .. thats using class literals.. thats clear. So basically this is exactly same as using Class<Foo> fooClass? fooClass is a variable reference of type Foo or an Object reference of type Foo? Thanks again.
Since you said it is representing AS AN OBJECT.. so fooClass should be a reference to an object of type Foo right?
No, fooClass is a of the type Class<Foo>. Added another little example to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.