Thursday, February 18, 2010

Overloading Main Method in Java

public class Simple
{
public static void main()
{
System.out.println("My Main");
} 
public static void main(String args[])
{ 
Simple.main();
}
}  
When you run this program your runtime calls main method
with string arguments first and then calls the main method
without arguments. The output of this program will be "My Main".

JAVA BASIC SYNTAX

class Classname
{
  public static void main(String args[])
  {
   //Actual program
   Statements;
  }
}

1. public - Access Specifier, properties and methods can access anywhere, 
any package.
2. main() - The Java environment starts the execution of a program from 
main() method.
3. void - The main() method is not return anything. So the return type of 
the main() method must be void.
4. static - The jave environment able to call the main() method without creating 
an instance of the class. So its declared as static.
5.(String args[]) - In java, whatever we give, it will takes as String. So we are 
giving the command line argument as String.

There is one question that arises: Why do 
we use String args[] when we don't pass an  
argument.The answer for this is runtime 
always calls the method with String argument.