Thursday, June 12, 2025
HomeJavaQuiz your self: Private and non-private courses and the Java Platform Module...

Quiz your self: Private and non-private courses and the Java Platform Module System


Java Platform Module System, Quiz, Oracle Java Career, Oracle Java Skills, Oracle Java Jobs, Oracle Java Preparation, Oracle Java

Given the modA.jar file with a correctly packaged modA module that has the next construction

└──modA

    │   module-info.class

    │   module-info.java

    │

    └───pkg

             Essential.class

             Essential.java

and given the next module definition

module modA {

}

and the next pkg.Essential class

package deal pkg;

class Essential {

  public static void predominant(String… args) {

    System.out.print(“Hello”);

  }

}

and the next command

java -p modA.jar -m modA/pkg.Essential

What’s the end result? Select one.

A. The command fails. To repair it it’s essential add the next line to module-info.java:

exports pkg;

B. The command fails. To repair it it’s essential add the next line to module-info.java:

opens pkg;

C. The command fails. To repair it it’s essential change the pkg.Essential class declaration to the next:

public class Essential {

D. The command efficiently runs and prints Hello.

Reply. One of many issues the Java Platform Module System (JPMS) addresses is encapsulation. With out the module system, Java’s encapsulation boundary selections are very restricted—and never notably sturdy. A personal Java component is probably the most constrained, however this degree of restriction might be bypassed utilizing reflection by default. Package deal-level accessibility might be bypassed just by putting a category in the identical package deal, as a result of packages might be break up throughout any variety of JAR recordsdata anyplace on the classpath. Plus, any public Java component is fully public all through the whole operating JVM.

That’s the place JPMS is available in. The system enforces encapsulation choices such that personal Java components are really non-public by default: Packages can’t be break up throughout modules, and a public component is accessible solely throughout the identical module until your deliberate directive opens the containing package deal to different modules.

Regardless of all this extra encapsulation, the JVM itself nonetheless has entry to every little thing. That’s crucial as a result of it mediates accesses between the weather of your code primarily based on the assorted directives within the supply code.

When the JVM needs to entry a predominant technique to launch a program, it doesn’t care if the category that incorporates that technique is public or not. It additionally doesn’t care whether or not the package deal containing the category is exported. Figuring out this, you may see the command proven for this query would run efficiently and print Hello to the console. Due to this fact, possibility D is right.

What concerning the incorrect choices?

Choice A suggests a must export the containing package deal. This might doubtlessly enable code from different modules entry to the general public options in that package deal, however as famous, that isn’t crucial for this code to run. Due to this fact, this selection is wrong.

Choice B can be incorrect as a result of opening a package deal doubtlessly permits reflective entry from code in one other module.

Choice C is wrong as a result of as with nonmodular code, the category containing the primary technique doesn’t should be public despite the fact that it normally is public as a matter of fashion.

Conclusion. The proper reply is possibility D.

Supply: oracle.com

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments