469,360 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

two main methods in a single program?

Hi,

Please tell me whether we can have two main methods in a single java program?
Sep 4 '07 #1
6 9790
JosAH
11,448 Expert 8TB
Hi,

Please tell me whether we can have two main methods in a single java program?
Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.    private double main= 54.42;
  3.    public Main() { }
  4.    public static void main(String[] args) {
  5.       Main main= new Main();
  6.       main.main();
  7.       main.main(42);
  8.       main("can");
  9.    }
  10.    private void main() { System.out.print("yes "); }
  11.    private void main(int i) { System.out.print("you "); }
  12.    private static void main(String s) { System.out.println(s); }
  13. }
  14.  
kind regards,

Jos
Sep 4 '07 #2
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.    private double main= 54.42;
  3.    public Main() { }
  4.    public static void main(String[] args) {
  5.       Main main= new Main();
  6.       main.main();
  7.       main.main(42);
  8.       main("can");
  9.    }
  10.    private void main() { System.out.print("yes "); }
  11.    private void main(int i) { System.out.print("you "); }
  12.    private static void main(String s) { System.out.println(s); }
  13. }
  14.  
kind regards,

Jos
Blech. That just looks awful.
Sep 4 '07 #3
kreagan
153 100+
Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.    private double main= 54.42;
  3.    public Main() { }
  4.    public static void main(String[] args) {
  5.       Main main= new Main();
  6.       main.main();
  7.       main.main(42);
  8.       main("can");
  9.    }
  10.    private void main() { System.out.print("yes "); }
  11.    private void main(int i) { System.out.print("you "); }
  12.    private static void main(String s) { System.out.println(s); }
  13. }
  14.  
kind regards,

Jos
1.) Why would you want 2 main functions? Are you trying to implement multithreading or something?

2.) Would you even consider that a main function: private static void main(String s) { System.out.println(s); } ? You are just overloading the main function, nothing more. The difference between a real main function and that overloaded imposter is: the program points and knows to point at the real main function. ... Am I making sense?

3.) That was really cute though.

To answer the poster's question. If you mean a method where the program starts, how can a program start at 2 different places? My answer would be no.
Sep 4 '07 #4
JosAH
11,448 Expert 8TB
1.) Why would you want 2 main functions? Are you trying to implement multithreading or something?
Huh? overloaded methods have nothing to do with multiple thread.

2.) Would you even consider that a main function: private static void main(String s) { System.out.println(s); } ? You are just overloading the main function, nothing more. The difference between a real main function and that overloaded imposter is: the program points and knows to point at the real main function. ... Am I making sense?
Nope, but the OP wasn't making sense either ;-)

3.) That was really cute though.
I know ;-)

To answer the poster's question. If you mean a method where the program starts, how can a program start at 2 different places? My answer would be no.
Yup, and my terrible example just shows that. The question didn't make sense.
Of course different classes can all have a static void main(String[] args) method.

kind regards,

Jos
Sep 4 '07 #5
JosAH
11,448 Expert 8TB
Blech. That just looks awful.
That Sir, is in the eye of the beholder ;-)

kind regards,

Jos
Sep 4 '07 #6
kreagan
153 100+
Huh? overloaded methods have nothing to do with multiple thread.
Sorry, I was refering to the first poster's question, not your crazy program. To me, it sounds like he wants to run 2 programs (threads) with 1 code.

Yup, and my terrible example just shows that. The question didn't make sense.
Of course different classes can all have a static void main(String[] args) method.
Right, but you can only run 1 at a time.
Sep 4 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
45 posts views Thread by Steven T. Hatton | last post: by
7 posts views Thread by Mark Kamoski | last post: by
29 posts views Thread by John Rivers | last post: by
13 posts views Thread by robinsonreyna | last post: by
12 posts views Thread by Andrew Poulos | last post: by
7 posts views Thread by Boki | last post: by
4 posts views Thread by chandu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.