WEBVTT

00:00.110 --> 00:00.650
Hi everyone.

00:00.650 --> 00:01.190
Welcome back.

00:01.190 --> 00:06.740
In this lecture we are going to learn about how to have another module reference a code from module

00:06.740 --> 00:07.040
one.

00:07.040 --> 00:12.020
So in this case we are going to use module two which is going to be referencing the module one service

00:12.020 --> 00:17.120
class by adding this project which is module one as a dependency to the module two.

00:17.150 --> 00:21.260
So in this case let's go ahead and open the module two Build.gradle file.

00:21.260 --> 00:24.020
So this is a build.gradle file for module two.

00:24.050 --> 00:25.700
So in this case we are going to be.

00:25.730 --> 00:28.130
You might notice this code also in your project.

00:28.130 --> 00:29.390
So let's uncomment this one.

00:29.390 --> 00:34.820
So what we are trying to do is we are trying to add the module one as a dependency to the module two,

00:34.850 --> 00:38.360
so that we get to access the source code from module one.

00:38.390 --> 00:43.730
So think of this as a client library which is going to use some of the code from module one.

00:43.730 --> 00:48.920
So now what I'm going to do I'm going to build a client package in the module two and try to reference

00:48.920 --> 00:51.020
the module one service from it.

00:51.050 --> 00:51.410
Okay.

00:51.440 --> 00:54.050
So here we are going to create a package.

00:54.080 --> 00:56.510
The package name is going to be client.

00:56.510 --> 01:01.250
And then I'm going to name this client class as module one client okay.

01:01.280 --> 01:07.560
Module one client, and in the module one client, I'm going to reference a module one service because

01:07.560 --> 01:09.090
I added this as a dependency.

01:09.090 --> 01:12.210
So I expect this module one service to be visible.

01:12.210 --> 01:21.960
So in this case let's go ahead and add private final module one service and module one service okay

01:21.990 --> 01:25.650
so I added this but still I have some compilation issues going on.

01:25.650 --> 01:28.200
Let's mouse over on that one add constructor parameter.

01:28.230 --> 01:28.860
There you go.

01:28.860 --> 01:30.480
So this code works as expected.

01:30.480 --> 01:33.540
What I'm going to do I'm going to introduce a function similar to this one.

01:33.570 --> 01:37.950
So in this case let's copy this function and then add it over here.

01:38.010 --> 01:40.890
But this function is not going to return the module directly.

01:40.920 --> 01:45.960
This is going to be using the module one service and call that specific function.

01:45.960 --> 01:49.290
So module one service dot retrieve data okay.

01:49.320 --> 01:50.970
And then add the semicolon over here.

01:50.970 --> 01:53.670
So this works as expected without any issues.

01:53.670 --> 02:00.060
So what we did so far is that we added a modular project into our project which doesn't use any modules

02:00.060 --> 02:00.720
concept.

02:00.720 --> 02:06.460
If you're using a setup like this, your code still gets executed in a module named Unnamed Module.

02:06.460 --> 02:11.110
I'll cover that in the future part of the section, but for now, what I'm going to do is I'm going

02:11.110 --> 02:17.200
to make this module two also to be modular and then show you the difference in the behavior.

02:17.200 --> 02:20.620
So what I'm going to do I need to create another module info file.

02:20.620 --> 02:22.750
So right click on this Java directory.

02:22.750 --> 02:25.210
So I'm in module two right now click on the file.

02:25.240 --> 02:29.920
It's going to be module hyphen info dot Java okay.

02:29.950 --> 02:34.000
So we can use that module info that we created from module one as a reference here.

02:34.000 --> 02:35.890
So in this case I'm going to put it on the right.

02:36.220 --> 02:36.940
There we go.

02:36.940 --> 02:41.560
And then what I'm going to do I'm going to create something similar.

02:41.560 --> 02:45.430
But in the case of this one this is going to be module two okay.

02:45.460 --> 02:47.470
So I'm going to remove all these things.

02:47.470 --> 02:50.290
It doesn't require any HTTP module two.

02:50.320 --> 02:52.690
So in this case after adding this right.

02:52.690 --> 02:55.870
So now let's go ahead and take a look at the module one client.

02:55.870 --> 03:00.520
So as soon as I go into the module one client I have issues over here.

03:00.520 --> 03:01.840
So what is the issue.

03:01.870 --> 03:09.330
It says like package comm dot module one dot service is declared in module module one, but module module

03:09.360 --> 03:10.950
two does not read it.

03:10.950 --> 03:16.770
So it is asking us to add the requires module one directive to the module-info.java file.

03:16.770 --> 03:18.690
In this case I'm going to add that one.

03:18.690 --> 03:19.920
So when I add that.

03:19.920 --> 03:25.290
Now if you go to the module info file which is part of the module two, you'll be able to see that requires

03:25.290 --> 03:25.710
directory.

03:25.710 --> 03:28.050
So as soon as you add this requires directive.

03:28.080 --> 03:30.720
This particular package gets resolved.

03:30.750 --> 03:37.170
Now the next thing is the module one also has something called secret something called HTTP client.

03:37.200 --> 03:37.800
Right.

03:37.800 --> 03:40.020
But these are not part of the exports.

03:40.020 --> 03:42.030
That is part of the module info file.

03:42.030 --> 03:43.680
That is part of module one.

03:43.680 --> 03:46.560
So in this case we are just exporting these two packages right.

03:46.560 --> 03:48.810
So what will happen if you try to access it.

03:48.810 --> 03:51.660
So in this case I'm going to open the module one client.

03:51.660 --> 03:57.270
And in this client I'm going to try to add the secret class in this case private secret.

03:57.270 --> 04:03.450
If you type secret there is no reference of that one because we are not even exporting that specific

04:03.450 --> 04:05.490
class as part of the module one.

04:05.490 --> 04:08.560
So now let's say if I go ahead and add that right.

04:08.590 --> 04:11.590
So in this case, if we add the secret package what will happen?

04:11.620 --> 04:15.820
So as soon as I add the secret package you'll be able to see that it is going to resolve.

04:15.820 --> 04:16.180
Fine.

04:16.210 --> 04:16.510
You see.

04:16.540 --> 04:16.810
Right.

04:16.810 --> 04:19.870
I'm able to get access to that secret package.

04:19.870 --> 04:22.240
So this is a behavior of exports.

04:22.240 --> 04:24.190
So now I'm going to remove the secret.

04:24.190 --> 04:27.460
And then you'll start to see the compilation issues showing up.

04:27.760 --> 04:30.040
So let's go ahead and remove the secret from here.

04:30.040 --> 04:33.190
So this is the advantage of using the modules concept.

04:33.190 --> 04:38.710
So you have complete control on what kind of packages you want to export and what kind of packages you

04:38.710 --> 04:40.060
don't want to export.

04:40.090 --> 04:40.450
Okay.

04:40.480 --> 04:48.040
So this is how you connect to different modules together using the Jpms concepts, which is also called

04:48.040 --> 04:49.210
modules concept.

04:49.210 --> 04:54.220
So we have created module one module file and module two module file.

04:54.220 --> 04:59.560
And this particular module specifically just uses a module one and implements this logic.

04:59.560 --> 05:03.190
So this is how you connect to modular projects.

05:03.220 --> 05:06.670
If you want to reference one code from the other module.

05:06.700 --> 05:08.260
This marks the end of this lecture.

05:08.260 --> 05:09.310
Thank you for watching.
