in endd effect main is a method like any other, however, since you will only have one public class in a file I believe the JVM will only be able to execute the main method from the public class making the main method in the other class irrelevant.
You most certainly can and it will work just fine (meaning that after you compile running java A will run the A's main method and running java B will run the B's main method). It is however, one of, if not THE, most unadvised thing(s) to do according to the java coding conventions.
Originally posted by Emanuel Kadziela: ...It is however, one of, if not THE, most unadvised thing(s) to do according to the java coding conventions.
Really? I've read that it's actually a recommended approach to faciliate testing during the development process. In fact, Bruce Eckel advocates this in Thinking in Java...
You can create a main( ) for each one of your classes, and it's often recommended to code this way so that your test code is wrapped in with the class. Even if you have a lot of classes in a program, only the main( ) for the class invoked on the command line will be called. (As long as main( ) is public, it doesn't matter whether the class that it's part of is public.) ... This technique of putting a main( ) in each class allows easy unit testing for each class. And you don�t need to remove the main( ) when you�re finished testing; you can leave it in for later testing.
Let's back up and see if we got the question right.
First, you can only put one top level class in each file. You can put nested classes in the same file, but you'll find nested classes can't have static methods, so no static main(). So you cannot have two classes with the standard main signature in one file.
Second, you can have a main on every class in your system. Before JUnit came along and stole our hearts this was a commmon way to write snippets of test code. Nowadays I'd rather have JUnit tests, but main for test and demonstration is still valid enough.
Third, it's probably bad form to call main() on another class. You can certainly do it, but it implies something missing in the other class design. It was designed to be called from the command line and you're calling it from another program. I've actually done this because it was the easy way to front-end a command-line utility with some setup and customization, and it definitely said to me the author of the utility failed to design an API I could use.
Last for now, you could overload main with different signatures and put several main() methods on one class. That would be very confusing to future readers which is a Bad Thing. The JVM will only call the exact signature that we all code. Any others would just be distracting.
Any of that hit the right spot?
Try to compile & run this and see what happens:
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Originally posted by Stan James: Let's back up and see if we got the question right.
First, you can only put one top level class in each file. [/CODE]
This is incorrect. You can put as many top level classes in a file as you wish. However, you can only have one public top-level class in the file. All of the classes can have a main() method. There really is nothing special about main() in and of itself. However, the JVM will only be able to run the main method in the public class.
You can have at most one (although you don't need any) top-level public class OR interface in a file.
If you do have a top-level public class or interface, then the file name must match that exactly.
If you don't have any top-level public class or interface definitions, then the file name can be anything -- i.e., it does not need to match any of the class or interface names (in which case you would compile using the file name, but run using the class name).