Hi,
Please tell me whether we can have two main methods in a single java program?
6 9973
Hi,
Please tell me whether we can have two main methods in a single java program?
-
public class Main {
-
private double main= 54.42;
-
public Main() { }
-
public static void main(String[] args) {
-
Main main= new Main();
-
main.main();
-
main.main(42);
-
main("can");
-
}
-
private void main() { System.out.print("yes "); }
-
private void main(int i) { System.out.print("you "); }
-
private static void main(String s) { System.out.println(s); }
-
}
-
kind regards,
Jos
-
public class Main {
-
private double main= 54.42;
-
public Main() { }
-
public static void main(String[] args) {
-
Main main= new Main();
-
main.main();
-
main.main(42);
-
main("can");
-
}
-
private void main() { System.out.print("yes "); }
-
private void main(int i) { System.out.print("you "); }
-
private static void main(String s) { System.out.println(s); }
-
}
-
kind regards,
Jos
Blech. That just looks awful.
-
public class Main {
-
private double main= 54.42;
-
public Main() { }
-
public static void main(String[] args) {
-
Main main= new Main();
-
main.main();
-
main.main(42);
-
main("can");
-
}
-
private void main() { System.out.print("yes "); }
-
private void main(int i) { System.out.print("you "); }
-
private static void main(String s) { System.out.println(s); }
-
}
-
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.
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
Blech. That just looks awful.
That Sir, is in the eye of the beholder ;-)
kind regards,
Jos
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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.
...
|
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.
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |