Java class object and class class

I have read this book on java about runtime type information and I am very confused about class objects and class class. The question may be silly, but I'm a little confused. When we usually write a class, we use the lower case c for the word class. eg. public class Foo with the class name starting in uppercase. therefore class Class is uppercase C for class name. Now the confusion is about the class object. which is also the upper case for this class. so now, for example, if we say that a method in a class like

public void countClass(Class<?>  type) {
 Class<?> superClass = type.getSuperclass();
}

public TypeCounter (Class<?> baseType) {
this.baseType = baseType;
}

      

Now in the first method I understand that the countClass method of type void has an argument of any type of class object? Class related to what exactly? CLASS class? or a class object? if it is a class object of any type, why can't we just write the name of the superclass and not complicate so much?

Does the second method have no type specified at all for the TypeCounter method? how it works? and again does it have the class type in the passed argument?

If they are of class Class, what does that mean? and if it is just a class object, then what is the class object doing here in this case?

There are such classes:

static class PetCounter extends LinkedHasMap<Class<? extends pet>,Integer> {
....}

      

is there really a class now? class or class? Sorry if I confuse you with this question. But try to answer.

Thanks in advance.

+3


source to share


5 answers


type

is a reference to a class of unknown type (hence <?>

).

Suppose you have a class Car

, you will get a runtime representation of that car, which will be passed to countClass. Then with getSuperClass you get the superclass Car

. It could be an object or something else.

The second is a constructor that has one type parameter Class

for an unknown type (hence <?>

).



The last example is a static named inner class PetCounter

that comes from a LinkedHashSet, which in turn only accepts objects of type Class that are instantiated by a pet. (I think pet

should be uppercase).

What it means: You can only add objects of type Class, which represent some pets.

You should look at it this way: In java, everything is an object except primitives (int, boolean, etc.). This means that if I call myObjectInstance.getClass()

, I get an object representing the meta information of that object. This object is of type Class

. Since I have several classes in my java project eg. Car

Vehilce

etc. I can further specify what this meta information will contain, so I can specify the type of these classes with a common one. If I do not know this, I can indicate <?>

.

+2


source


<?>

is a so-called substitution argument, meaning it can be any type of object without specifying a superclass. We could narrow it down by defining a superclass ( <? extends A>

) or a subclass ( <? super B>

), but in this case, we won't.



And the second method is actually a constructor, which is a kind of class template used to pass values ​​to initialize class fields. This is a fundamental concept of OOP, be sure to read more about it.

+1


source


A class type in java contains metadata (e.g. details, annotations, implemented class interfaces, etc.) of classes. It is not associated with any particular instance of a class, but is the same for all instances of a given class.

Whereas lowercase class is the reserved keyword for class declaration.

The first argument of the method uses the type Class, and this is not an instance, but the owner of the metadata created from

Class<?> metadata = AnyClass.class;

      

This probably won't answer all of your question, but hopefully gives you some thought.

+1


source


Let's take a look at the javadoc for the Class:

Instances of the Class class represent classes and interfaces to a running Java application ....

The class does not have a public constructor. Instead of class objects automatically using the Java virtual machine as the classes are loaded and calls the defineClass method in the class loader.

...

It is also possible to get a 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 is class

used to declare a class (class definition) or as a class literal (expression of a class object for runtime use)

public class Foo {}
Class<Foo> fooClass = Foo.class;

      

The class class

described 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 class

from an object instance:

Foo foo2 = new Foo();
Class<Foo> fooClass2 = foo2.getClass();

      

+1


source


First, you must know the difference between "class" and "class". We put similar things in the same category, that is, "class". For example, a car and a bicycle can be in a vehicle class. Of course, all classes can be placed in the same category, that is, "class". "Class" means the archetype of "class". Each class object has its own class, which stores its own information such as name, properties, methods, etc.

+1


source







All Articles