user708479
user708479

Reputation: 41

the number of class files in a java package constant

I am new to java.My friend asked me this question today And i am looking for an answer to it. How to make the number of class files in a package, constant? i.e., even though one can access that package,they should not be able to add any new class to the exisiting package.

Upvotes: 4

Views: 130

Answers (2)

mdma
mdma

Reputation: 57707

You want sealed packages. Once sealed, all classes from a package must come from the same JAR file. It basically boils down to adding the package to the manifest:

Name: myCompany/myPackage/
Sealed: true

Upvotes: 5

aioobe
aioobe

Reputation: 420951

This is called sealing the package and works on the level of jar files.

From the official trail:

Packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software.

To clarify: Since the classes must come from the same jar file, no one can add classes to your package, since the new classes wouldn't come from your jar file.

Upvotes: 3

Related Questions