The Class Class
A good measure of the complexity of an object-oriented
language is the degree of abstraction of its class structures. We know
that every object in Java is an instance of a class, but what exactly is a
class? In languages like traditional C++, objects are formulated by and
instantiated from classes, but classes are really just artifacts of the
compiler. In those languages, you see classes mentioned only in source
code, not at runtime. By comparison, classes in Smalltalk are real,
runtime entities in the language that are themselves described by
“metaclasses” and “metaclass classes.” Java strikes a happy medium between
these two languages with what is effectively a two-tiered system that uses
Class objects.
Classes in Java source code are represented at runtime by instances
of the java.lang.Class class. There’s a
Class object for every object type you
use; this Class object is responsible
for producing instances of that type. But you don’t generally have to
worry about that unless you are interested in loading new kinds of classes
dynamically at runtime or using a highly abstracted API that wants a
“type” instead of an actual argument. The Class object is also the basis for “reflecting”
on a class to find its methods and other properties, allowing you to find
out about an object’s structure or invoke its methods programmatically at
runtime. We’ll discuss reflection in the next section.
We get the Class associated with
a particular object with the getClass() method: ...