Technical discussions about .NET and J2SE Concepts

Can we have two main() methods in a Java Program

Hi…Greetings to Everyone!!!!

I am Jeyakumar.K, Asst. Professor, Dept. of CSE, Kumaraguru College of Technology-Coimbatore.  Here I am gonna discuss about calling main() from another main() in a single Java Program.

Don’t be too much surprised guys. Yes we can use two main() methods in a same program but not in the same class. But however, any one main() method would be the starting point of the program execution.

For Example:

import java.lang.*;
class SampleClassOne
{
public static void main(String[] args)
{
System.out.println(“SampleClassOne main() method is the starting point of Program Execution…”);
//To call another main() method
String[] strArray={“Java”,”Program”,”With”,”Two”,”Main”,”Methods”};
SampleClassTwo.main(strArray);
}
}
class SampleClassTwo
{
public static void main(String[] arg)
{
System.out.println(“\nSampleClassTwo main() method act like a normal method of the class…\n”);
for(int i=0;i<arg.length;i++)
{
System.out.println(arg[i]);
}
}
}

Execution Procedure:

— Save the above program in your local drive as SampleClassOne.java

— Compile the program by using: javac SampleClassOne.java

— Run the program by using: java SampleClassOne

— Then the output will be:

outputpaint

So Guys!!! Just go through the article alone is not enough…. Please try it out and Wish you all good luck!!!! ~ Jeyakumar.K

Education is learning what you didn’t even know you didn’t know.~Daniel J Boorstin”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s