gigadot
gigadot

Reputation: 8969

Why URLClassLoader.addURL protected in Java?

Is there any reason for URLClassLoader.addURL to be protected? I am asking this becuase I need to write a custom class load by extending the URLClassLoader and all it does is to call this protected method to add a jar file. If this wasn't pretected, then it could have been easier to add jar file dynamically.

Upvotes: 2

Views: 2570

Answers (1)

Stephen C
Stephen C

Reputation: 718826

Adding a new URL to a classloader is typically an abstraction-violating action, and can cause inexplicable things to happen. It is therefore hidden from normal usage.

You have an unusual use-case in which you have (I assume) concluded that the encapsulation violation is the lesser of the evils, and that it is OK to allow any part of an application to tinker with the innards of its parent classloader. Fair enough. And creating a custom classloader is the way to do this. But most folks wouldn't want that to happen.

Upvotes: 2

Related Questions