Saturday 15 February 2014

main method

In Java, every application must contain a main method. Program execution starts from main method.

Main method signature in Java is
    public static void main(String[] args)

public: main method should be called by JVM, which is not part of the project, so to make it available for the JVM, main method declared as public.

static: Before the main method is called, no objects are created. Having the static keyword means, the method can be called without creating any objects first.

void: In Programming languages, return codes tells the status of particular program/process/function execution. Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.

Java supports multi threading, so the main thread may finishes first, before other threads completes execution, So what status code the main thread returns, even it don't know about the executions of other threads started in main method. So in Java there is void return type for main method.

String args[]: Used to pass the command line arguments. Since you can convert the String values to any compatible type of primitive values like int, float, double etc.,

Some Points to Remember

1. Is overloading of main method valid ?
   Yes
class MainEx{
  public static void main(int a){
    System.out.println("in Integer " +a);
  }

  public static void main(double a){
    System.out.println("in double " +a);
  }

  public static void main(char a){
    System.out.println("in char " +a);
  }

  public static void main(String args[]){
    main(10);
    main(10.11);
    main('a');
  }
}
            
Output
in Integer 10
in double 10.11
in char a
            
2. You can call main method in any way like
     public static void main(String args[])
               (OR)
     static public void main(String args[])

3. Why is the Java main method static?
Before the main method is called, no objects are created. Having the static keyword means the method can be called without creating any objects first.

4. Why main method is public static in Java ?
main method should be called by JVM, which is not part of the project, so to make it available for the JVM, main method declaed as public.

Before the main method is called, no objects are created. Having the static keyword means the method can be called without creating any objects first.

5. Why main() in java void ?
In Programming languages, return codes tells the status of particular program/process/function execution. Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code.

Java supports multi threading, so the main thread may finishes first, before other threads completes execution, So what status code the main thread returns, even it don't know about the executions of other threads started in main method. So in Java there is void return type for main method.


6. Is the below program run?
    class MainEx{
        public static void main(){
        }
    }

Above program compiles fine, but Runtime, JVM tries to find the main method with signature
    public static void main(String args[])
there is no main method with the above signature in the program, so below run time error thrown

Error: Main method not found in class MainEx, please define the main method as:

    public static void main(String[] args)



Garbage Collection                                                 Command line Arguments                                                 Home

No comments:

Post a Comment