About me

Gladson A
Software Engineer,Chennai.

Translate

Pages

Saturday 30 June 2012

public static void main(String[] a) in JAVA

public static void main(String[] a)

Why we are declaring "public static void main(String[] a)" in JAVA programs.What is the purpose of declaring like this.

program:

public class test1 
{
public static void main(String[] a)
{
System.out.print("java main function");
}

}

output:

java main function

public        

       -(access control:public) allows everyone can access this method.
       
       -Mainly function main(String[] a) contains "public" because,It be Accessible to the JVM(JAVA VIRTUAL MACHINE) to begin execution of the program.

static

      - you may know that you need an object instance to invoke any method.

      -But,this method is Static because it be available for execution without an object instance.

void     

      -Once the main method execution is over that means that program end or terminates.So this method should not return any value.

      -void will not return any value.Hence "void" is declaring in the function main(String[] a)

main(String[] a)

      -The parameter "String[] a" is used to signify that the user may like to enter parameters to the java program at command line. We can use both String[] a or String a[]. The Java compiler would accept both forms

Deep Explaination about main(String[] a):

          public class test1 
{
   
  public static void main(String[] a) 
{
     
    if ( a.length <= 1 ) 
   {
      System.out.println("type arguments in Command line");
    } 
   else if ( a.length == 2 ) 
    {
      System.out.println( "your commands, " + a[0] + " " + a[1] );
    }
     
  }
   
}


In the console, command prompt or terminal add arguments in command line like this

java test1 sun moon

output:

your commands,sun moon

  Ask your Forums and Questions in the Comment box (mention your main Id).






Gladson,Gladson G-WorlD,Java.










GLADSON A
M.C.A Student
INDIA.
gladsinfo@gmail.com


3 comments: