Java: Difference between revisions

From miki
Jump to navigation Jump to search
(Miscellaneous)
Line 13: Line 13:
;[http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530 Class Literal]
;[http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530 Class Literal]
:A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a <code>.</code> and the token <code>class</code>. The type of a class literal is <code>Class</code>. 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.
:A class literal is an expression consisting of the name of a class, interface, array, or primitive type followed by a <code>.</code> and the token <code>class</code>. The type of a class literal is <code>Class</code>. 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.
:Eg:
{{lp2|<source lang="java" enclose="prevalid">
public class MyClass //...
//...
printf ("Class name is %s\n",MyClass.class.getName());
</source>}}

Revision as of 15:54, 10 February 2010

Sample Program

Class Test {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; i++)
      System.out.print(i = 0 ? args[i] : " " + args[i]);
    System.out.println();
  }
}

Miscellaneous

Class Literal
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.
Eg:
public class MyClass //...
//...
printf ("Class name is %s\n",MyClass.class.getName());