Chapter 10: Modules Flashcards
What is a module?
A module is a group of one or more packages plus a special file called module-info.java
Given a class in a package ‘zoo.animal.feeding’, create a module-info.java file with no body for that module.
file contents:
module zoo.animal.feeding { }
Where must the file module-info.java be placed?
In the root directory of the module.
Given a module ‘zoo.animal.feeding’ with a file Task.java, in the root directory ‘test’ and a directory ‘mods’ containing custom module files.
Compile the code.
javac -p mods test/zoo/animal/feeding/Task.java test/module-info.java
or
javac –module-path mods test/zoo/animal/feeding/Task.java test/module-info.java
Given a module ‘zoo.animal.feeding’ with a file Task.java in the root directory ‘test’ and a directory ‘mods’ containing custom module files.
Compile the code and place the class files in the directory ‘out’.
javac -p mods -d out test/zoo/animal/feeding/Task.java test/module-info.java
or
javac –module-path mods -d out test/zoo/animal/feeding/Task.java test/module-info.java
Suppose there is a module named book.module which was compiled in the directory ‘test’. Inside that module is a package named com.sybex, which has a class named OCP with a main() method.
Using the above information, run the module.
java –module-path test –module book.module/com.sybex.OCP
–module-path can be replaced by -p and –module can be replaced by -m
Package everything under the ‘test’ directory to the mods directory under the name zoo.animal.feeding
jar -cvf mods/zoo.animal.feeding.jar -C test/ .
Given the following module-info.java file, export the zoo.animal.feeding module so that the module will be available for other modules.
module zoo.animal.feeding { }
module zoo.animal.feeding { exports zoo.animal.feeding; }
Given the module zoo.animal.care, create a module-info.java file that exports the package zoo.animal.care.medical and uses the zoo.animal.feeding module.
module zoo.animal.care {
exports zoo.animal.care.medical;
requires zoo.animal.feeding;
}
Given the module zoo.animal.care, create a module-info.java file that exports the package zoo.animal.care.medical to only the module zoo.staff.
module zoo.animal.care { exports zoo.animal.care.medical to zoo.staff; }
What does the private access control mean with multiple modules?
The private access modifier applied to an element does not allow any outside modules to access the element.
What does the default (package-private) access control mean with multiple modules?
The default access modifier applied to an element does not allow any outside modules to access the element.
What does the protected access control mean with multiple modules?
Accessible to subclasses only if package is exported
What does the public access control mean with multiple modules?
Accessible only if package is exported
What does ‘requires transitive’ do?
While ‘requires’ specifies that the current module depends on another module, ‘requires transitive’ specifies that the current module depends on another module plus all the modules that the other module requires.