Geeks Talk

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.

main() method mystery

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

Go Back   Geeks Talk > Software Development > Java
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

Java Java related Issues and Problems

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-24-2009
Contributing Member
 
Join Date: Aug 2009
Location: bangalore
Posts: 30
Thanks: 11
Thanked 3 Times in 3 Posts
abhi981 is on a distinguished road
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-25-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 989
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
Re: main() method mystery

Quote:
Originally Posted by abhi981 View Post
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
Hi Abhijith,

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.
Reply With Quote
The Following User Says Thank You to rijus For This Useful Post:
  #3 (permalink)  
Old 09-25-2009
Contributing Member
 
Join Date: Aug 2009
Location: Indore
Posts: 33
Thanks: 9
Thanked 5 Times in 5 Posts
Sarje is on a distinguished road
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
Reply With Quote
The Following User Says Thank You to Sarje For This Useful Post:
  #4 (permalink)  
Old 09-29-2009
Junior Member
 
Join Date: Aug 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
dude_ravi1 is on a distinguished road
Re: main() method mystery

Hi Rijus,

But is it legal to override main() method

Last edited by dude_ravi1; 09-29-2009 at 12:38 PM.
Reply With Quote
  #5 (permalink)  
Old 09-30-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 989
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
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.
Reply With Quote
Reply

  Geeks Talk > Software Development > Java

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 06:20 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved