Tag: main method in java

main method in Java

What is main method in Java?

 

  • main method is a starting point in the program from where the code execution is started by the JVM.
  • If JVM does not find the main method inside the code according to the specifications configured inside it, it raises an error.

 

Syntax of main method in Java-

 

JVM always looks for the main method having the following signatures-

public static void main (String[ ] args)

 

  • main method is public so that it can be accessed from anywhere and is available to other programs like JVM.
  • main method is static so that it can be accessed even without objects and this allows JVM to access this method as there exists no object in the starting.
  • main method is void because it does not return anything to the JVM.
  • main method has been named main because this name is configured inside the JVM.
  • main method contains the arguments list String[ ] args which are “command line arguments” to store the values which are passed to the main method.

 

Note-

 

The above syntax is very strict and if we try to modify it, run time error is generated saying-

"Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)"

However, there are some acceptable changes which can be made.

 

Acceptable changes in the syntax of main method in Java-

 

Change-01:

 

Because in Java, the order in which the modifiers appear is not important, so instead of “public static“, we can comfortably write “static public“.

 

Change-02:

 

We can declare String[ ] args in any valid form such as-

  • String [ ]args
  • String args[ ]

 

Change-03:

 

Instead of args, we can use any valid java identifier.

 

Change-04:

 

In place of of String[ ], var-arg String parameter can be taken as String…

So, String[ ] args is same as String… args

 

Change-05:

 

We can also declare the main method with the following modifiers-

  • final
  • synchronized
  • strictfp

 

Rules to remember while using main method in Java-

 

Rule-01:

 

The concept of method overloading can be used for the main method but JVM will always execute only that main method implicitly which satisfies the specifications configured inside it.

It is the responsibility of the programmer to call other modified versions of the main method explicitly.

Example-

class Test
{
   public static void main(String[ ]  args)
   {
      System.out.println("main method called implicitly by the JVM");
   }

   public static void main(int[ ]  args)
   {
      System.out.println("main method called explicitly by the programmer");
   }
}

Output-

main method called implicitly by the JVM

This output clearly justifies our rule-01.

 

Rule-02:

 

We can use the concept of inheritance for all the static methods including the main method.

Thus, if there exists a child class not containing the main method, then while executing the child class, main method of the parent class will be executed.

 

Rule-03:

 

It appears as if the concept of overriding is applicable for static methods but it is not overriding, it is actually method hiding.

 

Rule-04:

 

It is possible to configure our own method in JVM instead of main method but for that customization of JVM is required i.e. some changes will have to be made inside the JVM.

 

Interview Questions based on main method in Java-

 

Question-01:

Is it possible to compile the java code successfully without main method?

Answer-

Yes, the java code compiles successfully even without the main method in it.

Reason-

Compiler is just responsible to check whether the code written follows the syntax rules or not. So, if the code is written following the correct syntax rules, it will not raise any error.

Example-

public class Test
{
   // statements
}

Clearly, the above class does not contain any main method.

Now, if we try to compile the above code, it compiles successfully without raising any error.

 

Question-02:

Is it possible to execute the java code successfully without main method?

Answer-

Although the java code compiles successfully even without the main method but it does not execute successfully without main method.

Reason-

At run time, JVM always looks for the main method having particular syntax which is configured inside it to start the execution of the program and if it does not find that main method, it raises an error.

 

NOTE-

Till JDK version 1.6, it was allowed to execute a class not containing a main method but from version 1.7 on wards, it has been made compulsory to have a main method inside the class for executing it.

 

Example-

public class Test
{
   // statements
}

Clearly, the above class does not contain any main method.

Now, if we try to execute the above code, it does not execute successfully and raises the following error-

Output-

"Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)"

 

Question-03:

Is it possible to print some statements successfully on the console without main method?

Answer-

  • It was possible till JDK version 1.6 only by writing those statements inside the static block because till version 1.6, the static blocks were executed irrespective of the fact whether we have main method inside our program or not.
  • But from JDK version 1.7 on wards, the static blocks are executed only if we have main method in our program.
  • So, now it is not possible to print any statement without having main method in our program.

 

Question-04:

Which of the following main method declarations are valid in java?

  1. public static void main (String… args)
  2. static final synchronized strictfp public void main (String[ ] args)
  3. public static void main (String args)
  4. final synchronized strictfp public void main (String[ ] args)
  5. public static void Main (String[ ] args)
  6. public static int main (String[ ]  args)
  7. final synchronized strictfp public static void main (String[ ] args)
  8. public void main (String[ ]  args)

Answer-

  1. Valid
  2. Valid
  3. Invalid (We need a String array to pass as argument and not String type variable)
  4. Invalid (static keyword is absent)
  5. Invalid (‘M’ must be small in spelling of main)
  6. Invalid (Return type of main method must be void)
  7. Valid
  8. Invalid (static keyword is absent)

Note-

  • In none of the above declarations, we will get compile time error. All declarations will compile successfully.
  • Except in (1), (2) and (7), we will get run time error.

 

Get more notes and other study material of Core Java.

Watch video lectures by visiting our YouTube channel LearnVidFun.