Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on main() method mystery within the Java forums, part of the Software Development category; Hi all, I have written following code. public class Sample { public static void main(String[] argv) { System.out.println(" Class Sample "); } } public class S { public static void ...
|
|||||||
|
|||
|
main() method mystery
Hi all,
I have written following code. public class Sample { public static void main(String[] argv) { System.out.println("Class Sample"); } } public class S { public static void main(String[] argv) { System.out.println("Class S"); } } I have saved the code in the file with name Sample.java . If I compile, it will print Class Sample. I know that file(program) can have only one public class. Now my doubt is how single program can have two different main() methods. Is it leagal in java.....? Thanks & Regards, Abhijit |
| Sponsored Links |
|
|||
|
Re: main() method mystery
Quote:
If u want to use more than one main method in a single pgm, make use of overriding concept. Here is the eg: class test { public static void main(String args[]) { System.out.println("welcome "); } } class test1 extends test { public static void main(String args[]) { System.out.println("geekinterview member"); } } class javaapplication1 { public static void main(String args[]) { String S[]=new String[10] ; test.main(S); test1.main(S); } } It ll give the output: welcome geekinterview member Regards, Rijus. |
| The Following User Says Thank You to rijus For This Useful Post: | ||
|
|||
|
Re: main() method mystery
Hi Abhi,
In a .java file each class can have its main() method, calling of the main() method by JVM depends on the given execution command In given example if we execute using following command java Sample then Sample class's main() method will be executed. and if command is java S then S class's main() method will be executed. The only benefit I can see in defining main() method in every class is we can execute each class. A class can not have multiple main() methods but a file can have as many main() methods as it has classes. hope it would help you |
| The Following User Says Thank You to Sarje For This Useful Post: | ||
|
|||
|
Re: main() method mystery
Hi Ravi,
One possible purpose of having multiple Main methods( overriding concept) in each class in a single source file is for debugging purposes. For the same reason, some Java developers like to write main methods for multiple classes within the same package, even though only one of them is meant to be executed. Regards, Rijus. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| main() method | Sarje | Java | 1 | 08-20-2009 09:13 AM |
| No Such Method Error : Main | ROHIT_4444 | Java | 1 | 06-23-2009 09:20 AM |
| Main method | divyargowda | Java | 1 | 10-23-2008 03:47 PM |
| applet using main method | manjula_p | Java | 1 | 02-12-2008 12:27 AM |
| Overload main method | Geek_Guest | Java | 6 | 01-22-2008 10:02 AM |