Explain public static void main in Java

Whether the class contains main method or not and whether main method is declared according to requirement or not these things won’t be checked by compiler.
At Run time JVM is responsible to check these things.

If JVM unable to find main method then we will get run time exception saying “No SuchMethodError: main”

class Test
{
}

Javac Test.java
Java Test
Runtime Exception: “No SuchMethodError: main”

At Run time JVM always searches for the main method with the following prototype.

public static void main (String[] args)

public:
public keyword is an access modifier which represents visibility, it means it is visible to all.
To call by JVM from anywhere.
This makes the main method public that means that we can call the method from outside the class.

static:
static is a keyword, if we declare any method as static, it is known as static method.
The core advantage of static method is that there is no need to create object to invoke the static method.
The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
Without existing object also JVM has to call this method.

void:
void is the return type of the method, it means it doesn't return any value.
Main method won’t return anything to JVM.

main:
It is the method name. This is the entry point method from which the JVM can run your program.
This is the name which is configured inside JVM and searched by JVM as a starting point for an application with a particular signature only.

String[] args:
Used for command line arguments that are passed as strings.
It is the parameter to the main Method.
Here we are defining a String array to pass arguments at command line. args is the variable name of the String array. It can be changed to anything such as String [] a.

Important Points to be Noted:

  • The above syntax is very strict, if we perform any change we will get Runtime Exception saying "NoSuchMethodError:main"
  • Even though the above syntax is very strict the following changes are acceptable.


  1. The order of modifiers is not important, that is instead of "public static" we can take "static public" also.
  2. We can declare "String[]" in any acceptable form. {main(String[] args), main(String []args), main(String args[])}
  3. Instead of 'args' we can take any valid Java Identifier.
  4. We can replace String[] with var arg parameter. {main(String[] args) = main(String... args) }
  5. We can declare main() method with the following modifier also. {final, synchronized, strictfp}
class Test
{
final static synchronized strictfp public void main(String... navdeep)
{
Sytem.out.println("Valid Test")
}
}

javac Test.java
java Test
Output: Valid Test

Question: Which of the following are valid main method declarations?
1. public static void main(String[] args)
2. public static void main(String... args)
3. public static final synchronized strictfp void main(String[] args)
4. public static void Main(String[] args)
5. public static int main(String[] args)
6. public static void main(String args)
7. public final synchronized strictfp void main(String[] args)

Answer:
1, 2, 3 are Valid
4 - Invalid (capital M in Main)
5 - Invalid (return type is int)
6 - Invalid (array[] is missing, String[] or String... is expecting)
7 - Invalid (static is missing which is compulsory)


Key Points to be noted:
1. Until 1.6 version if the class doesn't contain main() method then we will get runtime exception saying "No SuchMethodError: main". But from 1.7 version onwards we will get more meaningful error information "Error: main method not found in class test, please define main method as public static void main(String[] args)".
2. From 1.7 version onwards to run a java program main() method is  mandatory. Hence, even though class contains static blocks they won't be executed if the class doesn't contain main() method.

Example 1:
class Test
{
static
{
System.out.println("static block");
}
}

Output:-
1.6 version: static block and the Runtime Error.
1.7 version: Runtime Error

Example 2:
class Test
{
static
{
System.out.println("static block");
System.exit(0);
}
}

Output:-
1.6 version: static block and then exit. No Runtime Error.
1.7 version: Runtime Error

Example 3:
class Test
{
static
{
System.out.println("static block");
}
public static void main(String[] args)
{
System.out.println("main method");
}
}

Output:-

1.6 version:
static block
main method

1.7 version:
static block
main method

3. Without writing main() method is it possible to print some statements to the console?
Answer: Yes, we can print by using static block. But this rule is applicable until 1.6 version only. From 1.7 version onward main() method is mandatory to print statements on console.

4. For main() method Inheritance and Overloading concepts are applicable but "Overriding" concept is not applicable. Instead of that Method hiding concept is applicable.

Example 1: OVERLOADED METHOD
class Test
{
public static void main(String[] args)
{
System.out.println("String[]");
}overloaded methods
public static void main(int[] args)
{
System.out.println("int[]");
}
}
Output:
Java Test - String[]

Example 2: INHERITANCE
class P
{
public static void main(String[] args)
{
System.out.println("Parent main");
}
}
class C extends P
{
}
Output:
Java P - Parent main
Java C - Parent main

Example 3METHOD HIDING but not Overriding
class P
{
public static void main(String[] args)
{
System.out.println("Parent main");
}
}
class C extends P
{
public static void main(String[] args)
{
System.out.println("Child main");
}
}
Output:
Java P - Parent main
Java C - Child main

Happy Learning !

Comments

Post a Comment

Popular posts from this blog

What is Automation ?

OOPs concepts in Java with Examples