On Wednesday, 01/22/2003 at 10:12 EST, "W. Nathaniel Mills, III"
<[EMAIL PROTECTED]> wrote:
> A friend recently told me about the variable .class that can be used on
> any Class as in String.class or Hashtable.class, but not on instances of
> classes (e.g., String str = "Nat"; Class cls = str.class; // fails).

Not really a "field" -- just syntactic sugar they call a "class literal".

>From the JLS 2e:

   15.8.2 Class Literals

   A class literal is an expression consisting of the name of a class,
   interface,
   array, or primitive type followed by a `.' and the token class. The type
   of a
   class literal is Class. It evaluates to the Class object for the named
   type
   (or for void) as defined by the defining class loader of the class of
   the
   current instance.

It's a handy construct for uses like Reflection, where arrays of Class
are passed to indicate parameter types for reflected methods, as in:

    Constructor ctor = String.class.getConstructor(
        new Class[] {byte[].class, int.class}
    );

HTH.

-blair

Blair Wyman -- iSeries JVM -- (507) 253-2891
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"It is a sobering thought that when Mozart was my age,
he had been dead for two years." -- Tom Lehrer




____________________________________________________
To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

Be respectful! Clean up your posts before replying
____________________________________________________

Reply via email to