View Single Post
  #17 (permalink)  
Old 09-10-2008, 10:23 AM
masijade masijade is offline
Senior Member
 
Join Date: Jun 2008
Posts: 549
masijade is on a distinguished road
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.

Code:
// Filename BogusA.java package test; class BogusA { BogusA() { System.out.println("A"); } } class BogusB { BogusB() { System.out.println("B"); } }
Code:
// Filename BogusC.java package test; public class BogusC { public static void main(String[] args) { new BogusA(); new BogusB(); } }
Code:
// 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).
Reply With Quote