473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 9973
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

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
7
by: Mark Kamoski | last post by:
Hi Everyone-- Please help. What are the implications, (in terms of memory, application footprint, resource use, threading, and so forth), of using Shared methods? These Shared classes raise...
29
by: John Rivers | last post by:
Hello, What good reason there is for not allowing methods in ASPX pages I can't imagine, but here is how to get around that limitation: (START) <body MS_POSITIONING="FlowLayout"> <form...
13
by: robinsonreyna | last post by:
Hi everyone Is it possible to write a program which do not have a main() function. The program should compile and run. Please give sample code to do this.
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
7
by: Boki | last post by:
Hi All, When we delacre a main, why we dlacre it inside form? Why not outside of form? If form contains main(), why not every form contains a main()... Best regards, Boki.
4
by: chandu | last post by:
Hello, I had one doubt,please clarify it. Main is the entry point for the application. if i had multiple overloaded main methods in my application. how the compiler recognizes the original...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.