aspose file tools*
The moose likes Beginning Java and the fly likes main in multiple classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main in multiple classes" Watch "main in multiple classes" New topic
Author

main in multiple classes

Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Hi All,

Is it possible to have main() method in multiple classes for a single application ?


Regards,<br />Arnab
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Yes. There's nothing special about the name "main" -- a Java "main" is not compiled specially the way it is in C, for example. Every single class can have a main() if you want -- in fact, this can be a handy way to include test cases or usage examples in a class.

For a given running application, it's only the main() in the class named on your command line that matters. The other ones are ignored (unless you call them explicitly, which is perfectly legal.)


[Jess in Action][AskingGoodQuestions]
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Since theoretically, it is necessary for the java source file to have the same name as the class contaiing the main*() method, so theoretically, in a case where a single java file has multiple classes and each has main() method, what should be the name of the java source file ?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
If none of the classes are public, it doesn't matter. You could name the file Foo.java and the compiler wouldn't care.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Arnb Sen:
Since theoretically, it is necessary for the java source file to have the same name as the class contaiing the main*() method


Note that this is incorrect. There is absolutely no requirement linking the name of a source file to the name of a class containing a main() routine. As I said, public classes are required to be in like-named files, but that has nothing to do with whether there's a "main" or not.

You could have a file containing three classes, none of them public, each with a main(), and the file could have a name unrelated to any of the classes.
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Hi Ernest,

Thanks for the explanation. Everything is clear except one point --


You could have a file containing three classes, none of them public, each with a main(), and the file could have a name unrelated to any of the classes.


A java file contains 3 classes "abc", "def", "ghi" and "abc" is public. All the 3 classes have the method main().

1. What will be the name of the java file ?
2. To run the program, java abc or java def or java ghi - all 3 will work ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
You know, you could make a file like you describe, and see what happens when you attempt to compile it...


"I'm not back." - Bill Harding, Twister
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
tried. it compiles without any error. name of the file has to be "abc". Running it using Textpad results in invoking "java abc". If from dos prompt, I try "java def", it results in runtime error if the main() method (defined in the class def) is not public.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Arnb Sen:
tried. it compiles without any error. name of the file has to be "abc". Running it using Textpad results in invoking "java abc". If from dos prompt, I try "java def", it results in runtime error if the main() method (defined in the class def) is not public.


Note that the name of the file does NOT depend on the main() method. It only depends on which class was declared public. If you can understand this distinction, you should be okay.

With that said, it is probably a bad idea to have all these classes in a single file because you cannot use the other main() methods as you might expect. On the other hand, if you put class def in its own file and class ghi in its own file, each can have a main() method that you CAN run from the command-line individually.

HTH

Layne


Java API Documentation
The Java Tutorial
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Hi Layne,

Thanks.

Consier the case --

1. File "abc.java"
2. public class "abc" containing main() method
3. also, other classes in "abc.java" are "def" and "ghi" both containing main)_ method.

Can I call main() method of "def" or "ghi" from within "abc" just like an ordinary method call ? I guess this is possible.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Yes. There's nothing special about the "main" method; the fact that the application launcher uses it is just a convention (didn't I already say this in this thread?) In any case: you can call a main() method just as you'd call any other method.
Arnb Sen
Ranch Hand

Joined: Feb 23, 2004
Posts: 145
Thanks Ernest. That closes this chapter.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main in multiple classes