Technical discussions about .NET and J2SE Concepts

Core Java 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

Java Program without main() method

Hi Friends….Greetings to Everyone….

Can we able to write and execute a java program without writing a main() method? Any guesses….?

Yes friends…Its possible with the help of static block in java.

We all knew that, the heart and soul of the program execution is main() in java. But its true only in the case when you are not having static block in your java program.

Because static block of statements are getting executed at the first instant by the Java Interpreter.

For Example – Program with both static block and main():

class StaticBlockDemo
{
//Static Block Definition
static
{
System.out.println(“Statements inside Static Block……”);
System.out.println(“Statements inside Static Block……”);
}//End of static block

public static void main(String[] args)
{
System.out.println(“You are in Main Function – Starting Point of Program Execution”);
}
}//End of class

When you are executing the above program, you can find that static block of statements getting executed first and then the main() method starts its execution.

Java Program: Without writing main()

import java.lang.*;  //Optional
class StaticBlockWithoutMainDemo
{
//Static Block Definition
static
{
System.out.println(“Statements inside Static Block……”);
System.out.println(“Statements inside Static Block……”);
System.exit(0);
}//End of static block
}//End of class

Save the above program with StaticBlockWithoutMainDemo.java in your local drive.Then compile and run the program in a normal way.

Thank You Friends!!!