sandeeprajsingh tandon wrote:When i write a class as an application developer, the compiled class "MyClass" goes into the classpath . System ClassLoader should automatically load that class when needed.
Is my above understanding correct?
Correct.
Now when you say
"If the JAR is not part of the class path" : Then where do you see it lying?
The most often used example is with plugins. The user should be able to add plugins without recompiling the application, and in an ideal situation also without having to modify the class path. Just let him put the JAR file in a specific folder, load that JAR file with a URLClassLoader, then load the class. Or in an even more generic way, let the user specify the location of the JAR file without having to copy it. The JAR file can even be located on some web server!
"and the class cannot already be found by the thread's current class loader" : Which is the threads current class Loader and why was it not found by that class loader,
Each thread can have its own class loader. By default it's the system class loader but with Thread.setContextClassLoader you can change it.
Now consider again the plugin example. My thread's current class loader is still the system class loader. The JAR file is not in the class path. That means that the thread's class loader cannot find the class. That's why you use a URLClassLoader.
"two distinct calls to the same method with the same parameters still lead to different classes," : Hence i didnt understand this quote of yours.
Two Class objects are equal if they have the same name and are loaded by the same class loader. In my example both classes have the same name, but each is loaded using a newly created class loader. Therefore, even though both Class objects have the same name and come from the same JAR file, they are still not equal.