Yes you can use mulitple classes declared inside a file out side of that "file". You cannot use them outside of the declared package. They are, by default, package protected (same as any class that is not specifically assigned an access modifier). Case in point, try these three files.
// Filename BogusA.java
package test;
class BogusA {
BogusA() {
System.out.println("A");
}
}
class BogusB {
BogusB() {
System.out.println("B");
}
}
// Filename BogusC.java
package test;
public class BogusC {
public static void main(String[] args) {
new BogusA();
new BogusB();
}
}
// Filename BogusD.java
package test.subtest;
import test.BogusA;
import test.BogusB;
public class BogusD {
public static void main(String[] args) {
new BogusA();
new BogusB();
}
}
C works since it is in the same package as A and B (don't even need imports as is normal for classes in the same package), but D does not, since it is in another package (which also helps to illustrate that "subpackages" have no extra permissions to their "parents" as to any other package, and vice-versa).
Declaring multiple classes in the same file is not, necessarily, a "good programming practice", but it is perfectly valid. The
only restriction, is that there can only be one
public class (but not that there must be).