Shripathi said:
class is a property of an object of the class Object. It is in a way
similar to the length property for arrays.
Well, sorta.
The .class syntax designates a class literal, and it should be applied
to a class name, not to an object reference (i.e. MyClass.class, not
instanceOfMyClass.class). Compilation of such a literal creates a class
reference in the output class file, and the literal itself resolves to
an object of class Class representing the named class. The existence of
the specified class is checked at compile time, but the class might not
actually be loaded at runtime if no expression involving the class
literal ever needs to be evaluated.
Class.forName(String) is an invocation of a static method of class
Class, which attempts to find, and if necessary load, the named class at
runtime. If successful, it returns an object of class Class
representing the named class. The existence of the named class is not
checked at compile time, but when invoked the method forces the class to
be loaded if it hasn't already been.
The differences are in fact subtle, as you already appreciate if you are
asking the question. As a rule of thumb, the latter is always used if
the name of the class to load is not known at compile time (e.g. a
plug-in), and may sometimes be used in compatability code where the
source makes conditional use of a class that is only available in some
of the Java versions it supports. (A specific example being code that
must support both 1.1-1.3 style focus management and 1.4+ focus
management: the two schemes are not compatible, but you can't compile
with or run on 1.3 if you explicitly use the new 1.4 stuff. You can
work around it with conditional loading of the 1.4 classes, but you
cannot use class literals lest the compilation fail on 1.3.) On the
other hand, where it is possible to get away with it, it is cleaner,
prettier, and just generally better to use the literals.
Examination of the API docs for class Object will show that it has no
field named "class" (although it has an instance method getClass()).
Similarly, array objects do not actually have a field named length.
There is actually a bytecode instruction specifically for returning the
length of an array, and compilation of a construct such as
myArray.length produces bytecode involving that instruction. Thus the
..class and .length syntaxes do indeed have similarities, but not quite
the kind that you described.
Class.forName("YourClass") will work without you having an instance of the
YourClass class. It will return an instance of a Class object that you can
then use to instantiate an object of YourClass. (Using newInstance)
Right.
In order to use .class, you will need an instance of the YourClass class.
Wrong. If you have an object, you can invoke getClass() on it, but you
form a class literal by appending ".class" to the class name, with not
instance required.
The rest, and a more accurate description can be had from the JLS or the
javadocs
And the Java Virtual Machine Specification could be handy, too, but
start with the JLS. (JavaDocs have little to say because they don't
describe array classes and ".class" is not in fact a property of Object.)
John Bollinger
(e-mail address removed)