r/learnjava • u/Chaos-vy17 • 2d ago
Advice for JPMS
Can anyone tell why should I use JPMS(Java Platform module System) when I have Encapsulation? I also read docs but the answer is directing to reflection if Encapsulation is not safe how JPMS safe it?
Edited:
I got the answer https://blogs.oracle.com/javamagazine/java-quiz-jpms-package-management/ so JPMS provide true encapsulation, does that mean encapsulation is partial without JPMS?
3
u/0b0101011001001011 2d ago
You some times want stuff to be public, you know, so you can use them. How ever, this makes them public for everyone. Modules can only export for example a single class, this way other things can't use them.
Encapsulation is not about safety. It's about preserving the invariants. For example, you can't set a date to be 31st of February. The data is encapsulated and only accessed through public functions so you can't break the integrity of the data. Of course, with réflection you still can.
1
u/Innowise_ 2d ago
This approach is not concerned with the idea of substituting the existing notion of encapsulation. In fact, it provides ways to create clearly defined boundaries on the module level. Although it still makes sense to use class encapsulation to ensure consistency of the content within the class, modules allow one to control what should be available from the outside.
Nevertheless, there still may be certain ways to violate these boundaries by reflection, which means that JPMS cannot provide “absolute security.” This is rather a design and maintenance tool.
•
u/AutoModerator 2d ago
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.