This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
I am loading class Test2 from class Test using Class.forName().
But when I try to get the classLoader for class Test2, i get null value.
Both classes are in the same folder.
Class Test2 :
Class Test :
Can anyone please explain why I am getting classloader as null.
Should'nt it return the same classloader as that of class Test.
The API documentation of Class.getClassLoader() says:
Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.
Abhineet Kapil wrote:Should'nt it return the same classloader as that of class Test.
Did you try checking what the classloader is that was used to load class Test? Maybe it's also null. Try: Test.class.getClassLoader()
The difference you are seeing is when you are calling getClass().getClassLoader() on a Class object. This is asking for the class loader that loaded the Class object (which is presumably the bootstrap class loader) and not the class loader that loaded the test2 class.
IF you change your code to:
You will get the answer you expected.