Tuesday, August 8, 2017

Java 9 : The Future 2

Modules enforce Strong Encapsulation as we discussed in the previous post. Now a Package is not public by default to all other modules unless it is explicitly exported in the module-info.java Module Descriptor.

Can Class D from Module A access Class F from Module B?

With the Module Descriptors mentioned below, yes it is possible as Module A has mentioned as it requires Module B and Module B has exports package Z.

Strong Encapsulation

Module must require other Modules to be used in them.

Module exports packages to specific Modules or to all.

Module opens packages explicitly for reflection.

Accessibility is enforced by compiler, JVM and Reflection.

But why Modules again?

Remember packages starting with sun.* or com.* that were available in rt.jar (JVM Runtime) which were only specific for certain JVM Vendor Implementations and not all. Java programmers have been constantly warned not to use these packages as they will break Platform Independence. But up until Java 9 Modules there was no way to completely stop developers from using those packages.

Enter java.base Module

Following is the Module Descriptor for java.base module. With this as you can see only certain packages are exported and com.*/sun.* packages are only available within the module.

// module-info.java

module java.base {

exports java.lang;

exports java.io;

exports java.net;

exports java.util;

}

Every Module in Java 9 implicitly will have require to java.base. Just like every class in Java will automatically import java.lang.*.

So what does this means?

This means that now there can be a Java Application which is 100% modular with all Jar files being modules. Which means we can't use classpath in those scenarios.

No comments:

I am a seasoned Software Engineer with Proven Experience in Java based Software Development. I Provide Consultation or Freelance Development with High Quality Standards. Feel free to contact me at shazin (dot) sadakath (at) gmail.com or by visiting IoT Researcher
If you found the contents in this blog useful, then please make a donation to keep this blog running. You can make donations via Skrill with email address shazin.sadakath@gmail.com