This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between contextClassloader and Class.forname

 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can anyone tell me the differnece between
thread.currentThread().getContextClassloader().loadClass("<class-name>")
and
class.forname("<class-name>")?

I am really confused between this two. I went through the javadoc also. I am also not aware of what does thread.currentthread.contextClassLoader() will return? Doen't the contextclassloader and class's classLoader are same?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kartik Patel:
Doen't the contextclassloader and class's classLoader are same?


No, certainly not.

The current class's ClassLoader is whatever loaded that class and is fixed.

The thread context ClassLoader is under your control; you can set it. If you haven't set it yourself, it defaults to the thread context ClassLoader of the thread that started the current thread. If the current thread is the main thread, the default context ClassLoader is the system ClassLoader (the one that works with the classpath).

In applications with multiple ClassLoaders, I tend to find that Class.forName() usually does the wrong thing. I tend to use the thread context ClassLoader, which I have set appropriately.
[ July 28, 2006: Message edited by: Peter Chase ]
 
Kartik Patel
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter for your reply.

Just to add to this I now have the clear concept of the difference between these two.

If anyone is still confuse, he/she can go through this link.
This topic extensively explained and clear many other doubts of classloading

Understanding Class.forName()
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please DontWakeTheZombies. I am not sure that I agree with what you have written, either. But please don't continue the discussion in this thread. If you want to discuss further, please start a new thread.

(Looks like the zombie-waker has now deleted his post)
[ February 29, 2008: Message edited by: Peter Chase ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic