Java ClassLoader delegation model?

前端 未结 6 1496
悲哀的现实
悲哀的现实 2021-02-01 08:35

When calling loadClass() on a ClassLoader, does the ClassLoader first check if the class has been loaded, or does it immediately delegate this check to

6条回答
  •  再見小時候
    2021-02-01 09:11

    The two statements are not exactly mutually exclusive. The Class will only exist in the current ClassLoader's set of loaded classes if the parent ClassLoader had previously failed to find the Class. So,

    When requested to find (the external data that describes) a class or resource, a ClassLoader instance will delegate the search for (the external data that describes) the class or resource to its parent class loader before attempting to find (the external data that describes) the class or resource itself.

    Which does not prevent it from short-circuiting if it knows its parent can't find the class but it can (as shown by it previously loading the class)

提交回复
热议问题