• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main in multiple classes

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Is it possible to have main() method in multiple classes for a single application ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If none of the classes are public, it doesn't matter. You could name the file Foo.java and the compiler wouldn't care.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, you could make a file like you describe, and see what happens when you attempt to compile it...
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest. That closes this chapter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic