Can any one help me on.. main () method...

Can an application have multiple classes having main method? If yes how can I call other main methods.. i mean apart from the one called by JVM ? And can I even call method named main or it could b only called by JVM.Can I do something like overriding main or overlaoding main
[299 byte] By [Shebua] at [2007-11-27 9:06:05]
# 1
main(String[] args) is a method like any other, there's nothing special about it except that the VM is instructed to look for it as the main point to the application.You can't override or overload it since it's static, but you can call main methods from other classes as you
-Kayaman-a at 2007-7-12 21:41:06 > top of Java-index,Java Essentials,Java Programming...
# 2

Maybe your question was satisfactorily answered already? I'll add a comment or two in case.

Some people frown on having more than one main method. It can be confusing, since the main methods signals the entry point of the program, of which, as you said, there can be only one in a given launch.

Occasionally I write a main method in a class for doing a simple test of that class. So to test the class, I tell the JVM to call the main method in the class. To run the program, I tell it to call the main method in my main class. The people I mentioned above will argue that I should build JUnit tests instead. I also sometimes do that, it's a wonderful tool; only, ocasionally I consider it overkill and go with the main method instead.

In any case, I believe the common use of more than one main method (if there is a common use at all) is the opportunity of starting the program at different points at different times. I have never seen one method calling a main method, even though, as has been said, you can do it if you want.

OleVVa at 2007-7-12 21:41:06 > top of Java-index,Java Essentials,Java Programming...
# 3
> Can I do something like overriding main or> overlaoding main ?You are aware that you can't override any static method, aren't you?
CeciNEstPasUnProgrammeura at 2007-7-12 21:41:06 > top of Java-index,Java Essentials,Java Programming...
# 4

On the other hand, overloading should work fine as with any method, I suppose. Not that I would see any point in it, it would easily get even more confusing. The JVM will insist on calling the one that takes a single String[] argument only. In other words, only yourself can call other versions.

OleVVa at 2007-7-12 21:41:06 > top of Java-index,Java Essentials,Java Programming...
# 5

> In any case, I believe the common use of more than

> one main method (if there is a common use at all) is

> the opportunity of starting the program at different

> points at different times.

can u please put on some more light and explain a bit "the opportunity of starting the program at different

points at different times."

sabyasachi.roya at 2007-7-12 21:41:07 > top of Java-index,Java Essentials,Java Programming...
# 6

> can u please put on some more light and explain a bit

> "the opportunity of starting the program at

> different

> oints at different times."

Forget about that. It just means that you can have multiple main methods that would start the program, for whatever reason. I doubt there would be one, and if there were, you'd know what to do.

CeciNEstPasUnProgrammeura at 2007-7-12 21:41:07 > top of Java-index,Java Essentials,Java Programming...
# 7

> can u please put on some more light and explain a bit

> "the opportunity of starting the program at

> different

> oints at different times."

I was referring back to what I said about putting in a main method for test purposes.

In one utility class, I wrote a main method that called various methods in the class, printed out the results along with my comments about expected results. The disadvantage is it takes time to read through the printout and verify results agree with expectations. Still, it's comforting to do this whenever I've made a change in the class. My utility class is now used in a number of different programs. Clearly, you when part of a larger program, you should not start from the main method in the utility class, but from the main method in that larger program.

For more than just a little testing, I recommend JUnit tests, they save you from reading through results only to verify they're fine. Instead, JUnit tells you the tests all passed or which one(s) failed.

OleVVa at 2007-7-12 21:41:07 > top of Java-index,Java Essentials,Java Programming...
# 8
> I was referring back to what I said about putting in> a main method for test purposes.> ya i got it..u mentioned it for test purpose..thx
sabyasachi.roya at 2007-7-12 21:41:07 > top of Java-index,Java Essentials,Java Programming...