What does classname.class return?

It returns the same what Object.getClass() does for a given instance, but you can use it when you know statically what class you want (i.e. at compile time).

From the Javadoc:

Returns the runtime class of this Object.

In short, it gives you an object that represents the class of the (original) object. It’s used, amongst other things, by reflection when you want to programatically discover methods and fields in order to invoke/access them.

For example:

        Method m[] = String.class.getDeclaredMethods();
        for (int i = 0; i < m.length; i++)
        {
          System.out.println(m[i].toString());
        }

The Javadoc also refers you to the Java Language Specification – Class Literals (which might be a little heavy reading).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)