Search code examples
javanullclassloader

When does Class#getClassLoader return null?


Say I have some Java code:

public class Widget {
    ...whatever
}

And some code that classloads the Widget:

ClassLoader widgetLoader = Widget.class.getClassLoader();

Can widgetLoader ever be null? Why/why not? If so, under what circumstances?


Solution

  • According to this method javadoc:

    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.