On 3/14/2025 3:54 AM, Simone Bordet wrote:
As a side question, I was wondering what is the benefit of using
ModuleLayers versus just using ClassLoaders.

As Alan mentioned, layers are how you get strong encapsulation, where the accessibility of classes in modules is enforced by the VM. This is not the same thing as isolating classes in different loaders, where reflection can be used to access any class.

Please watch 15 minutes of my JVMLS 2016 presentation, starting at:

https://www.youtube.com/watch?v=QnMDsI2GbOc&t=1479s

"Creating a layer informs the module system about the packages that will exist in the program, and the modules they'll come from. The module system passes this information to the VM, so that when classes are eventually loaded and defined, the VM knows which module a class belongs to, and what that module reads and exports. It's like the VM has a shadow copy of the module graph that it uses when checking accessibility between classes."

Alex

Reply via email to