Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write package-statements for nested packages?

If I have a class in a sub-package like sub-package-name, which is in a parent-package like higher-level-package-name, do I just write

package "sub-package-name";

Or also add the parent-package, maybe like

package "sub-package-name";
package "higher-level-package-name";

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Example

You are only supposed to write a single package statement, with the full name of the package. So if you have a directory hierarchy for your packages like:

foo/
└─ bar/
   └─ baz/
      └─ Test.java

Your Test.java file would have a statement like:

package foo.bar.baz;

Nested packages

Java itself actually has no concept of nested packages.

As far as the language is concerned,

  • foo,
  • foo.bar and
  • foo.bar.baz

are considered three independent packages. Sub-packages or parent-packages are not a thing to Java.

Consequently, package-visibility also does not carry over to sub- or parent-packages.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading