The Java documentation for Class
says:
Class
objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to thedefineClass
method in the class loader.
What are these Class
objects? Are they the same as objects instantiated from a class by calling new
?
Also, for example object.getClass().getName()
how can everything be typecasted to superclass Class
, even if I don't inherit from java.lang.Class
?
A Class object is an instance of Class (java.lang.Class). Below quote taken from javadoc of class should answer your question. 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 ...
The java. lang. Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
Everything in Java is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. A Class is like an object constructor, or a "blueprint" for creating objects.
In computer programming, the object class refers to a class created to group various objects which are instances of that class. Classes are code templates for creating objects. In cases where objects need to be grouped in a certain way, an object class is the "container" for a set of objects built on these templates.
Nothing gets typecasted to Class
. Every Object
in Java belongs to a certain class
. That's why the Object
class, which is inherited by all other classes, defines the getClass()
method.
getClass()
, or the class-literal - Foo.class
return a Class
object, which contains some metadata about the class:
and some useful methods like casting and various checks (isAbstract()
, isPrimitive()
, etc). the javadoc shows exactly what information you can obtain about a class.
So, for example, if a method of yours is given an object, and you want to process it in case it is annotated with the @Processable
annotation, then:
public void process(Object obj) { if (obj.getClass().isAnnotationPresent(Processable.class)) { // process somehow; } }
In this example, you obtain the metadata about the class of the given object (whatever it is), and check if it has a given annotation. Many of the methods on a Class
instance are called "reflective operations", or simply "reflection. Read here about reflection, why and when it is used.
Note also that Class
object represents enums and intefaces along with classes in a running Java application, and have the respective metadata.
To summarize - each object in java has (belongs to) a class, and has a respective Class
object, which contains metadata about it, that is accessible at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With