This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
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.