Skip to content

Commit d4046f2

Browse files
committed
Modulese
1 parent d9aeb3d commit d4046f2

15 files changed

+279
-17
lines changed

src/SUMMARY.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ Zombie Game
566566
- [Public Classes](./packages/public_classes.md)
567567
- [Fully Qualified Class Name](./packages/fully_qualified_class_name.md)
568568
- [Import](./packages/import.md)
569+
- [Package Imports](./packages/package_imports.md)
569570
- [The Default Package](./packages/the_default_package.md)
570571
- [The Anonymous Main Class](./packages/the_anonymous_main_class.md)
571572
- [Public Methods](./packages/public_methods.md)
@@ -840,27 +841,32 @@ Make them do one. -->
840841
- [Modules](./modules.md)
841842
- [Declaration](./modules/declaration.md)
842843
- [Restrictions](./modules/restrictions.md)
843-
- [Exports]()
844-
- [Integrity]()
844+
- [Exports](./modules/exports.md)
845+
- [Requires](./modules/requires.md)
846+
- [Module Imports](./modules/module_imports.md)
845847
- [java.base](./modules/java.base.md)
846848
- [The Unnamed Module](./modules/the_unnamed_module.md)
847-
- [Module Imports](./modules/module_imports.md)
848849
- [Multi-Module Directory Layout](./modules/multi_module_directory_layout.md)
850+
- [Purpose](./modules/purpose.md)
851+
- [Challenges](./modules/challenges.md)
852+
<!--
849853
- [Lambdas](./lambdas.md)
850854
- [Functional Interfaces](./lambdas/functional_interfaces.md)
851855
- [@FunctionalInterface](./lambdas/functional_interface_annotation.md)
852856
- [Lambda Expressions](./lambdas/lambda_expressions.md)
853857
- [Method References](./lambdas/method_references.md)
854-
- [Runnable]()
855-
- [Function]()
856-
- [Relation to Checked Exceptions]()
858+
- [Runnable](./lambdas/runnable.md)
859+
- [Function](./lambdas/function.md)
860+
- [Checked Exceptions](./lambdas/checked_exceptions.md)
861+
- [Challenges](./lambdas/challenges.md)
862+
857863
- [Streams](./streams.md)
858864
- [map](./streams/map.md)
859865
- [filter](./streams/filter.md)
860866
- [Collectors](./streams/collectors.md)
861867
- [toList](./streams/toList.md)
862868
- [mapMulti](./streams/mapMulti.md)
863-
- [Gatherers](./streams/gatherers.md)
869+
- [Gatherers](./streams/gatherers.md) -->
864870

865871
# Sharing Code
866872

src/lambdas.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Lambdas
22

3+

src/lambdas/challenges.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Challenges

src/lambdas/checked_exceptions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Checked Exceptions

src/lambdas/function.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Function

src/lambdas/runnable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Runnable

src/modules/challenges.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Challenges
2+
3+
Remember the rules for this are
4+
5+
- Try to use only the information given up to this point in this book.
6+
- Try not to give up until you've given it a solid attempt
7+
8+
## Challenge 1.
9+
10+
Add a `module-info.java` to one of your projects.
11+
12+
## Challenge 2.
13+
14+
Move some of your code to use the multi-module directory layout.
15+
If you need to split one of your projects into multiple modules
16+
to pull this off, do so.
17+
18+
## Challenge 3.
19+
20+
Intentionally create a split package situation. What error does Java give you?
21+
22+
## Challenge 4.
23+
24+
Intentionally create a cycle in the "module graph." What error does Java give you?

src/modules/exports.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Exports
2+
3+
Inside your `module-info.java` file you declare which packages are
4+
"exported" from that module.
5+
6+
```java
7+
module reality {
8+
exports earth;
9+
exports sea;
10+
exports sky;
11+
}
12+
```
13+
14+
Any packages that do not have an `exports` declaration are "unexported packages."
15+
16+
The classes in unexported packages will not be visible to other modules
17+
even if those classes are `public`.
18+
19+
```java,no_run
20+
// Because the "backrooms" package is not exported
21+
// from the "reality" module, other modules cannot
22+
// observe classes within it.
23+
package backrooms;
24+
25+
public class YellowCarpet {
26+
}
27+
```
28+
29+
```java,no_run
30+
package sky;
31+
32+
33+
// But classes within other packages of
34+
// the "reality" module can see it just fine.
35+
public class Cloud {
36+
private final YelloCarpet fabricOfExistence
37+
= new YellowCarpet();
38+
39+
// ...
40+
}
41+
```

src/modules/module_imports.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
A special kind of import is the "module import."
44

5-
Just like a `*` at the end of a regular import will
5+
Just like a package import (`import package_name.*`) will
66
import all the classes in that package, an `import module`
77
declaration will import all the classes in all the packages
88
contained within that module.
@@ -24,4 +24,9 @@ class Main {
2424
IO.println(c);
2525
}
2626
}
27-
```
27+
```
28+
29+
This has much the same upsides and downsides as a package import. It is
30+
much easier to write this and get all the classes you want, but in exchange
31+
it might be harder to see where a particular class comes from when you are
32+
reading code later.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
11
# Multi-Module Directory Layout
2+
3+
If you yourself want to develop a project using multiple of your own modules
4+
there is a special way to layout the files to do so.
5+
6+
First, make folders with the name of each module. If a module name
7+
has a `.` in it the folder should too.
8+
9+
```text,no_run
10+
reality/
11+
backrooms/
12+
horrible.monsters/
13+
```
14+
15+
Then in those folders under a `src` folder put the `module-info.java` files.[^src]
16+
17+
```text,no_run
18+
reality/
19+
src/
20+
module-info.java
21+
backrooms/
22+
src/
23+
module-info.java
24+
horrible.monsters/
25+
src/
26+
module-info.java
27+
```
28+
29+
From there you can put all the classes you want into each module, so long as they don't conflict
30+
or create split packages.
31+
32+
```text,no_run
33+
reality/
34+
src/
35+
earth/
36+
Dirt.java
37+
Worm.java
38+
sea/
39+
Starfish.java
40+
sky/
41+
Cloud.java
42+
module-info.java
43+
backrooms/
44+
src/
45+
backrooms/
46+
YellowCarpet.java
47+
module-info.java
48+
horrible.monsters/
49+
src/
50+
horrible/
51+
monsters/
52+
Slime.java
53+
Skeleton.java
54+
module-info.java
55+
```
56+
57+
This can be helpful in structuring larger projects.
58+
59+
[^src]: The `src` folder isn't technically required. You will see what a `--module-source-path` looks like in a bit. I think its a good idea anyways.

0 commit comments

Comments
 (0)