tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Preißer <verlag.preis...@t-online.de>
Subject [OT] Querstion about Class.forName() re. ClassLoaders
Date Fri, 10 May 2013 16:41:04 GMT
Hi all,

I apologize for being completely off-topic (this question has nothing to do with Tomcat),
but I thought there may be some guys here that are experts in class loading and are able to
answer my question.


You probably know the method java.lang.Class.forName(String name) which returns a class object
from the given name. The JavaDoc (Java 7) of this method says:

      Returns the Class object associated with the class or interface with the given string
name. Invoking this method is equivalent to:
          Class.forName(className, true, currentLoader) 
      where currentLoader denotes the defining class loader of the current class.

While this description may be a bit ambiguous about what "current class" means here, the JavaDoc
of Class.forName(String name, boolean initialize, ClassLoader loader) makes it clearer by
stating:

      For example, in an instance method the expression:
          Class.forName("Foo") 
      is equivalent to:
          Class.forName("Foo", true, this.getClass().getClassLoader())


So, shouldn't this mean that I get the same result/behavior when writing this code:

    public void doSomething() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("foo.Bar");
        // do something...
    }

Instead of this one:

    public void doSomething() throws ClassNotFoundException {
        Class<?> clazz = Class.forName("foo.Bar", true, this.getClass().getClassLoader());
        // do something...
    }

or did I misunderstand something?

Thanks!


Regards,
Konstantin Preißer


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message