Interview Question : Explain about public static void main(String args[])

class demo

{

public static void main(String args[])

{

System.out.println(" welcome to java world ");

}

}

Public:

  1. access modifier
  2. public means that is available across packages.
  3. if you remove public  then it’s not allowed to be executed , it means that main method has to be public.
  4. JVM can easily find the main method if you are use public .
class demo
              {

static void main(String args[])

{

System.out.println(" welcome to java world ");

}

}

If you are try to run this program , will get compile time error

Class demo does not have a main method.

  • static

  1. you can call main method with out creating the object of the class.
  2. So that JVM can load the class into memory and call the main method.
  3. If u remove static from main method,JVM can not able to call  because there is no object of the class.
  4. main is a static method. This means main method is part of its class and not part of objects.

class demo

{

public  void main(String args[])

{

System.out.println(" welcome to java world ");

}

}

If you are try to run this program , will get compile time error

Class demo does not have a main method.

  • void

1.Every method has a return type and here main method return type is void

means it wont return any thing to jvm.

2. if you are change the return type as int you wil get the compile time error.

public class demo

{

public static int main(String args[])

{

System.out.println(" welcome to java world ");

return 0;

}

}

If you are try to run this program , will get compile time error

main

1.name given to the main method

2. is it possible to change the main method name?

           NO.


 

public class demo

{

public static void karthik(String args[])

{

System.out.println(" welcome to java world ");



}

}

If you are try to run this program , will get compile time error

String[] args:

Or

String args[]:

Or

String  []args:

1. main method will accept single argument of type String array

Or  array of object of the class String

2. This is also know as java command line arguments

3. is it possible to change the name args: yes

public class demo

{                                                              

public static void main(String karthik[])

{

System.out.println(" welcome to java world ");



}

}

karthik receives any command-line arguments present when the program is executed.

karthik is a variable which is defined as a identifier.

Posted on by