Main Method in Java

The main method is the entry point to a desktop based core Java application.

Previously, we created our first Java program "Hello.java" as:

public class Hello {

  public static void main(String[] args)

  {

     System.out.println("Hello");

  }

}

We then compiled it using javac as:

javac Hello.java 

We then executed it as:

java Hello

And output was:

Hello

Though we did not tell which method to execute, Java executed the main method and printed "Hello". This is because main method is the entry point to a desktop based core Java application.

 

Signature of the entry main method

This entry main should be of the signature:

public static void main (String [] args)

Each element of the signature can be explained as below:

  • public - public is the least restricted access modifier. Because jvm, which is outside our program will have to invoke main, we need to give public.

  • static - static methods and variables belong to class and be called without creating object using just the class name. Making main method as static allowes JVM to invoke main without creating an object of your class.

  • void - as we are not returning anything back to JVM, we specify the return type as void.

  • String args[] - This String array parameter is for accepting any command line arguments, if passed. Command line arguments are any extra parameters you add to your java command line after the class name. (Note: The name argas doesn't matter here and can be anything.)

 

Can we have more than one main method in a class?

Yes,  we have more than one main method in our class through overloading just like any other method. The main method is just like any other method, but Java will look for a main method with the signature 'public static void main(String args[])' to start the execution of the program. We can call the other main methods (or even this one) ourselves just like any other method.

 

Quiz

Question 1

Given:

class MyClass{

public static void main (String args) {

System.out.println(args[1]);

}

}

Find output if executed as:

java MyClass Hello World

 

Question 2

Given:

class MyClass {

public static void main (String [] args) {

System.out.println(args[2]);

}

}

Find output if executed as:

java MyClass Hello World

 

Question 3

Given:

class MyClass {

public static void main (String [] args) {

System.out.println (args);

}

}

Find output if executed as:

java MyClass Hello World

 

Question 4

Given:

class MyClass {

public static void main (String [] args) {

System.out.println (args [1]);

}

}

Find output if executed as:

java MyClass Hello World

 

Answers

Answer 1

Will not compile.

String array is required, not a string as the parameter.

 

Answer 2

 We will get java.lang.ArrayIndexOutOfBoundsException.

We are passing only two command line arguments Hello and World, which should be accessed using indexes 0 and 1.

 

Answer 3

This will print: [Ljava.lang.String;@192d342

 

Answer 4

This will print: World

We are passing only two command line arguments Hello and World, which should be accessed using indexes 0 and 1.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!