Java package does not match path
Say we have a Java source file with the following package definition at the beginning:
package evil.weylandyutani.xenomorph;
, but the file lives in the following directory: …/evil/weylandyutani/xenomorph/alien. So, the path to the file does not correspond to the package components.
I always thought you can’t do that, but actually, it’s possible. Maven won’t complain when compiling.
NetBeans marks this as a warning in the file, the popup says Incorrect package. Eclipse, on the other hand, signals an error which percolates up to the top level.[1]
So, yes, you can put a file into a package that does not correspond to the directory path. But I won’t do it, for the following reasons:
-
Why do it? I don’t see any advantage in breaking the rules[2] here.
-
When the package and the file path match, it’s easier to find the file. Just follow the path to find the file.
-
Some tools are not ok with it, so why discuss with them?