Monday 21 August 2017

public static void main (String args [] ) in Java

Main method in java is entry point for any core java program. Remember we are not talking about servlet, MIDlet or any other container based managed java program where life cycle method are provided to control the execution.

public static void main( String [] ar )

public - Java used this
because its called by the JVM outside the program. If we make it private, protected or default type JVM can not access it thats why its define as public. JVM can easily access and execute it.

static - Since main method is static Java virtual machine can call it without creating any instance of class which contain main method.

Anything which is declared in class in java comes under reference type and require object to be created before using them but static method and static data are loaded into separate memory inside JVM called context which is created when a class is loaded. If main method is static than it will be loaded in JVM context and are available to execution.

void - The program may exit before or after the main method finisher; a return value from main method would therefore be meaningless. If you want the program to return a status code call one of the following methods :
                                            System.exit (int status) - Runtime.getRuntime().exit (status)

String [] ar - In Java ar contains the supplied command-line arguments as an array of String objects.

you can define main method using many way which java support. Signature of main method in java :

public static void main (String ar [] )
public static void main( String [] ar )
public static void main ( String ...ar ) - Here its use varargs.
public static synchronized void main ( String ...ar )
public static strictfp void main (String ...ar )
public static final void main (String ...ar )

Please write your review and any suggestion. Thank you .

No comments:

Post a Comment