Hi everyone.
Welcome back.
In this lecture we are going to learn about how to have another module reference a code from module
one.
So in this case we are going to use module two which is going to be referencing the module one service
class by adding this project which is module one as a dependency to the module two.
So in this case let's go ahead and open the module two Build.gradle file.
So this is a build.gradle file for module two.
So in this case we are going to be.
You might notice this code also in your project.
So let's uncomment this one.
So what we are trying to do is we are trying to add the module one as a dependency to the module two,
so that we get to access the source code from module one.
So think of this as a client library which is going to use some of the code from module one.
So now what I'm going to do I'm going to build a client package in the module two and try to reference
the module one service from it.
Okay.
So here we are going to create a package.
The package name is going to be client.
And then I'm going to name this client class as module one client okay.
Module one client, and in the module one client, I'm going to reference a module one service because
I added this as a dependency.
So I expect this module one service to be visible.
So in this case let's go ahead and add private final module one service and module one service okay
so I added this but still I have some compilation issues going on.
Let's mouse over on that one add constructor parameter.
There you go.
So this code works as expected.
What I'm going to do I'm going to introduce a function similar to this one.
So in this case let's copy this function and then add it over here.
But this function is not going to return the module directly.
This is going to be using the module one service and call that specific function.
So module one service dot retrieve data okay.
And then add the semicolon over here.
So this works as expected without any issues.
So what we did so far is that we added a modular project into our project which doesn't use any modules
concept.
If you're using a setup like this, your code still gets executed in a module named Unnamed Module.
I'll cover that in the future part of the section, but for now, what I'm going to do is I'm going
to make this module two also to be modular and then show you the difference in the behavior.
So what I'm going to do I need to create another module info file.
So right click on this Java directory.
So I'm in module two right now click on the file.
It's going to be module hyphen info dot Java okay.
So we can use that module info that we created from module one as a reference here.
So in this case I'm going to put it on the right.
There we go.
And then what I'm going to do I'm going to create something similar.
But in the case of this one this is going to be module two okay.
So I'm going to remove all these things.
It doesn't require any HTTP module two.
So in this case after adding this right.
So now let's go ahead and take a look at the module one client.
So as soon as I go into the module one client I have issues over here.
So what is the issue.
It says like package comm dot module one dot service is declared in module module one, but module module
two does not read it.
So it is asking us to add the requires module one directive to the module-info.java file.
In this case I'm going to add that one.
So when I add that.
Now if you go to the module info file which is part of the module two, you'll be able to see that requires
directory.
So as soon as you add this requires directive.
This particular package gets resolved.
Now the next thing is the module one also has something called secret something called HTTP client.
Right.
But these are not part of the exports.
That is part of the module info file.
That is part of module one.
So in this case we are just exporting these two packages right.
So what will happen if you try to access it.
So in this case I'm going to open the module one client.
And in this client I'm going to try to add the secret class in this case private secret.
If you type secret there is no reference of that one because we are not even exporting that specific
class as part of the module one.
So now let's say if I go ahead and add that right.
So in this case, if we add the secret package what will happen?
So as soon as I add the secret package you'll be able to see that it is going to resolve.
Fine.
You see.
Right.
I'm able to get access to that secret package.
So this is a behavior of exports.
So now I'm going to remove the secret.
And then you'll start to see the compilation issues showing up.
So let's go ahead and remove the secret from here.
So this is the advantage of using the modules concept.
So you have complete control on what kind of packages you want to export and what kind of packages you
don't want to export.
Okay.
So this is how you connect to different modules together using the Jpms concepts, which is also called
modules concept.
So we have created module one module file and module two module file.
And this particular module specifically just uses a module one and implements this logic.
So this is how you connect to modular projects.
If you want to reference one code from the other module.
This marks the end of this lecture.
Thank you for watching.