> 
> > If you know the class' name, and I suspect that you do 
> > since you are in a static method of the class, you can 
> > do a Class.forName(className).
> 
> Better still, className.class, and have it resolved at
> compile time rather than by a runtime lookup. 
> 

Miles, "className.class" is just a short-hand for this code: 

        {
            ...
            if (_className == null) {
                _className = getClassName("className");
            }
            ...
        }

        private static Class _className;
        private static getClassName(String cname) {
            try {
                return (Class.forName(cname));
            } catch (ClassNotFoundException _) {
                throw new NoClassDefFoundError(_.getMessage());
            }
        }

In other words, you're saving a try/catch clause at the expense of
one hidden static variable.

For example, this a.java

    public class a {
        Class x() {
            return (a.class);
        }
    }

is compiled into:

    public class a extends java.lang.Object {
        static java.lang.Class class$a;
        public a();
            /* Stack=1, Locals=1, Args_size=1 */
        static java.lang.Class class$(java.lang.String);
            /* Stack=3, Locals=2, Args_size=1 */
        java.lang.Class x();
            /* Stack=2, Locals=1, Args_size=1 */
    }

    Method a()
       0 aload_0
       1 invokespecial #8 <Method java.lang.Object()>
       4 return

    Method java.lang.Class class$(java.lang.String)
       0 aload_0
       1 invokestatic #12 <Method java.lang.Class forName(java.lang.String)>
       4 areturn
       5 astore_1
       6 new #5 <Class java.lang.NoClassDefFoundError>
       9 dup
      10 aload_1
      11 invokevirtual #13 <Method java.lang.String getMessage()>
      14 invokespecial #9 <Method java.lang.NoClassDefFoundError(java.lang.String)>
      17 athrow
    Exception table:
       from   to  target type
         0     5     5   <Class java.lang.ClassNotFoundException>

    Method java.lang.Class x()
       0 getstatic #11 <Field java.lang.Class class$a>
       3 ifnull 12
       6 getstatic #11 <Field java.lang.Class class$a>
       9 goto 21
      12 ldc #1 <String "a">
      14 invokestatic #10 <Method java.lang.Class class$(java.lang.String)>
      17 dup
      18 putstatic #11 <Field java.lang.Class class$a>
      21 areturn


Hope that helps,

        - Godmar


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to