Reputation: 73
So imagine this folder structure.
main\
\pack1
a\A.java
b\B.java
A.java is not in the same folder as B.java. But both have the same package statement as following:
package main.pack1;
With that package statement, they have no compiler error, if i try to access protected fields from each other. If I have the package statement as following:
package main.pack1.a;
package main.pack1.b;
for the two Files respectivly, I cannot access the protected fields of each other, as expected.
The question is, is it good practice to group classes from different folders into one package?
I want to structure the .java files in a folder structure, for better organisation (so that i dont have like 20 files in one folder), but they should be in the same package, for usage of protected fields.
I hope its clear, what im asking.
Upvotes: 0
Views: 76
Reputation: 32914
There is no requirement from the standard javac
compiler for any specific organization of source code files, but if your folder structure doesn't mirror the package structure you would be violating a (practically) universal convention. Reasons you would not want to do that include:
tl;dr : Conventions exist for a reason, and usually it's best to follow them unless there is an ovewhelming reason to go against the grain. Such cases exist, but "I don't like 20 files in one folder" wouldn't be one of them IMNSHO.
Upvotes: 1