It is incorrect to say that there is no such thing as a subpackage in Java. The term is defined in the Java Language Specification (JLS) and has been since the initial version of the specification.
That said, there is no difference in accessibility for a member in a subpackage vs. one in an arbitrary unrelated package.
There is therefore no way within the language to make a method visible to two different packages in the requested manner, while restricting its access from other packages.
Subpackages
The members of a package are its subpackages and all the top level class types and top level interface types declared in all the compilation units of the package.
For example, in the Java SE Platform API:
- The package
java
has subpackages awt
, applet
, io
, lang
, net
, and util
, but no compilation units.
- The package
java.awt
has a subpackage named image
, as well as a number of compilation units containing declarations of class and interface types.
The subpackage concept has practical implications, as it enforces naming constraints between packages and classes/interfaces:
A package may not contain two members of the same name, or a compile-time error results.
Here are some examples:
- Because the package
java.awt
has a subpackage image
, it cannot (and does not) contain a declaration of a class or interface type named image
.
- If there is a package named
mouse
and a member type Button
in that package (which then might be referred to as mouse.Button
), then there cannot be any package with the fully qualified name mouse.Button
or mouse.Button.Click
.
- If
com.nighthacks.java.jag
is the fully qualified name of a type, then there cannot be any package whose fully qualified name is either com.nighthacks.java.jag
or com.nighthacks.java.jag.scrabble
.
However, this naming restriction is the only significance afforded to subpackages by the language:
The hierarchical naming structure for packages is intended to be convenient for organizing related packages in a conventional manner, but has no significance in itself other than the prohibition against a package having a subpackage with the same simple name as a top level type declared in that package.
For example, there is no special access relationship between a package named oliver
and another package named oliver.twist
, or between packages named evelyn.wood
and evelyn.waugh
. That is, the code in a package named oliver.twist
has no better access to the types declared within package oliver
than code in any other package.
Solutions
Since subpackages provide no special access relationship, there is no way within the language to make a method visible to two different packages in the requested manner, while restricting its access from other packages.
Either the method can be made public and all packages (including odp.proj
and odp.proj.test
) will be able to access the given methods, or the method could be made package private (the default visibility), and all the code that needs to directly access it must reside in the same (sub)package as the method.
Regarding the test use case, a standard practice in Java is to put the test code for a type in the same package as its source code, but in a different location on the file system. For instance, in both the Maven and Gradle build tools, the convention would be to put the source files in src/main/java/odp/proj
and the test files in
src/test/java/odp/proj
. When compiled by the build tool, the items in both directories end up in the odp.proj
package, but only the src
files are included in the production artifact; the test files are only used at build time to verify the production files. With this setup, test code can freely access any package private or protected code of the code it's testing, as they will be in the same package.
In the case where you want code sharing across subpackages or sibling packages that isn't the test/production case, one solution I've seen some libraries use is to put that shared code as public, but document that it is intended for internal library use only.