/* While this file and program does not illustrate good style - at least not with regard to the basic programming principles taught in this AP Java class - it can be convenient for efficient for development and testing to put multiple classes in one file */ public class MultipleClasses { public static void main(String[] args) { System.out.println("public class MultipleClasses - main"); Enemy darthVader = new Enemy(); Player lukeSkywalker = new Player(); } } class Enemy { private int myX; public Enemy() { myX = 1; System.out.println("Enemy has been instantiated"); } public static void main(String[] args) { System.out.println("does nothing"); } } class Player { private int myX; public Player() { myX = 3; System.out.println("Player object has been instantiated"); } } /* You cannot make two or more of the classes in the same file "public" for the following reason. Although, you can have more than one method named "main" in the same file. One reason is that some RISC processor architectures (like those Sun uses mostly) have hierarchical memory architectures. This hierarchy is divided into global modules and local (global-accessible) submodules. The advantage of this is that the processor needs lesser memory access "points" (handles), as the modules delegate CPU calls to the submodules. The java compiler can utilize this and increase performance by loading the entire source files into the memory and assign them a CPU handle (the module ID). It'll be later used for linking, e.g. The public class in a file will be loaded as a module and gets the handle, all other non-public classes in that file will be submodules. If there is no public class, a generic module will be used, so that's no problem. But if there are multiple public classes, you'd end up with several modules and just one handle to assign, thus having ambiguity. The CPU won't be able to address the correct module. This all only applies to RISC CPUs, but for obviuos cross-platform compatibility reasons it was added to the standard - it doesn't hurt the other architectures, but helps those with hierarchical memory management. The main() method is just another method... public class Foo { public static void main(String[] args) throws Exception { Bar.main(args); } } class Bar { public static void main(String[] args) { args = new String[]{"Rang", "Dang", "Doo"}; Baz.main(args); } } class Baz { static void main(String[] args) { System.out.println(join(args)); } static String join(String[] args) { StringBuilder buf = new StringBuilder(); for (String s : args) { buf.append(s + ' '); } return buf.toString(); } } */