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

  • main method is a standard method used by JVM to execute any java program(Java SE).
  • Lets see an example class without main method.

  1. package instanceofjava;
  2. public Demo{
  3.  
  4. }


  • This class will compile fine but while executing JVM fails to find the main method in that class
  • So it will throw an exception: NoSuchMethodError:main

  1. package instanceofjava;
  2. public Demo{
  3. public static void main(String [] args){
  4.  
  5. }
  • This program will compile and executed. Because at run time JVM calls the main method on that class.

Why main method is public?

  •  To call by JVM from anywhere main method should be public.
  • If JVM wants to call our method outside of our package then our class method should be public.

  1. package instanceofjava;
  2. public MainDemo{
  3. public static void main(String [] args){
  4.  
  5. }
  6. }

public static void main(string args) interview Questions

Why main method is static?

  • To execute main method without creating object then the main method oshould be static so that JVM will call main method by using class name itself.

Why main method return type is void?

  • main method wont return anything to JVM so its return type is void.

 Why the name main?

  • This is the name which is configured in the JVM.

Why the arguments String[] args?

  •  Command line arguments.
  • String array variable name args : by the naming conventions they named like that we can write any valid variable name.

Can we write static public void main(String [] args)?

  • Yes we can define main method like static public void main(String[] args){}
  •  Order of modifiers we can change.

  1. package instanceofjava;
  2. public MainDemo{
  3. static public void main(String [] args){
  4.  
  5. }
  6. }

What are the possible public static void main(String [] args)method declaration with String[] args?

  • public static void main(String[] args){ }
  • public static void main(String []args){ }
  • public static void main(String args[] ){ }
  • public static void main(String... args){ } 
  • Instead of args can place valid java identifier.

Can we declared main method with final modifier?

  • Yes we can define main method with final modifier.

  1. package instanceofjava;
  2. public MainDemo{
  3. public static final void main(String [] args){
  4.  
  5. }

Main method with all possible modifiers:

 

  1. package instanceofjava;
  2. public MainDemo{
  3. static final synchronized public void main(String [] args){
  4.  
  5.  System.out.println("Main method");
  6.  
  7. }

Output:

  1. Main method

Overloading main method:

  • We can declare multiple main methods with same name as per overloading concept.  
  • So we can overload main method but every time JVM looks for main method with String[] args method.
  • Lets see an example program on main method overloading.
  • main(String[] args) method will be called automatically by JVM.
  • If we want to call other methods we need to call explicitly.

 

  1. package instanceofjava;
  2. public MainDemo{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method String [] args");
  6.  
  7. public static void main(int[] args){
  8.  
  9.  System.out.println("Main method int[] args");
  10.  
  11. }
  12.  





Output:

  1. Main method String [] args


Main method Inheritance:

  • Inheritance concept is applicable for main method.
  • If a sub class not having main method and extending a super class which is having main method.
  • Then if we execute super class main method will be executed fine.
  • If we execute sub class which is not having main method also executed fine and super class main method will be called.

  1. package instanceofjava;
  2. public A{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method of class A");
  6.  
  7.  



Output:

  1. Main method of class A

 

  1. package instanceofjava;
  2. public B extends A{
  3.  
Output:

  1. Main method of class A

What happen if both super and sub class having main method?

  • Actually its not overriding concept here. it is a method hiding concept in this scenario regarding with main method.

  1. package instanceofjava;
  2. public A{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method of class A");
  6.  
  7.  

Output:

  1. Main method of class A


  1. package instanceofjava;
  2. public B extends A{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method of class B");
  6.  
  7.  


Output:

  1. Main method of class B

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

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

  1. A nice content. We can know many things from here. Keep going to write many things like this.
    Thank You.
    You may visit this site Valobashar Sob Rong

    ReplyDelete
  2. Many of my ambiguities are clarified after reading this page. Thank you so much.. :)

    ReplyDelete
  3. nice answer and explain very clearly.thank u so much.....

    ReplyDelete

Select Menu