Originally posted by Martin Simons:
...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.
No, you can execute main from a class with default access, so one file can contain as many class definitions with main methods as you like. (But only one public, of course.)
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.
Are you referring to these Java coding conventions from Sun? I've skimmed these, but I didn't see anything on this topic.
[ March 28, 2006: Message edited by: marc weber ]
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:
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.