Main Method Interview Question in java

Main Method Interview Question in java

Please find the list of Main Method Interview Question in java :

What is the main method in Java?

Main method in Java is entry point for any Java program.In core Java program, execution starts from main method when you type java main-class-name, JVM search for public static void main(String args[]) method in that class and if it doesn’t find that method it throws error NoSuchMethodError:main and terminates.

 

Why JVM search for public static void main(string []args)?

JVM is construct like that whenever we run any java program it will search for psvm(strng args) to execute program.

 

What will happened if we not declare main method as a static?

The compiler will treat as an instance method, i.e. you won’t be able to execute it directly with java YourClass.

 

Can We Overload main() method?

Yes, We can overload main() method. A Java class can have any number of main() methods. But to run the java class, class should have main() method with signature as “public static void main(String[] args)”. If you do any modification to this signature, compilation will be successful. But, you can’t run the java program. You will get run time error as main method not found.

public class MainMethod

{

public static void main(String[] args)

{

System.out.println(“Execution starts from this method”);

}

 

void main(int args)

{

System.out.println(“Another main method”);

}

 

double main(int i, double d)

{

System.out.println(“Another main method”);

 

return d;

}}

 

Can we declare main() method as private or protected or with no access modifier?

No, main() method must be public. You can’t define main() method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM. If you define main() method other than public, compilation will be successful but you will get run time error as n  o main method found.

public class MainMethod

{

private static void main(String[] args)

{

//Run time error

}

}

 

Can We Declare main() Method As Non-Static?

No, main() method must be declared as static because keyword static allows main() to be called without creating an object of the class in which the main method is defined. If we omit static keyword before main Java program will successfully compile but it won’t execute.

 public class MainMethod    

{

 

public void main(String[] args)

{

System.out.println(1);         //Run time error

}

}

 

Why main() method must be static?

Suppose, If main() is allowed to be non-static The compiler will treat as an instance method, i.e. you won’t be able to execute it directly with java YourClass

public class MainMethod 

{

public MainMethod(int i)

{

//Constructor taking one argument

 

public void main(String[] args)

{

//main method as non-static

}

}

Can we change return type of main() method?

No, the return type of main() method must be void only. Any other type is not acceptable.

public class MainMethod

{

public static int main(String[] args)

{

return 1;    //run time error : No main method found

}

}

 

Can main() method take an argument other than string array?

No, argument of main() method must be string array. But, from the introduction of var args you can pass var args of string type as an argument to main() method. Again, var args are nothing but the arrays.

public class MainMethod

{

public static void main(String… args)

{

//Var args as an argument

}

}

 

Can we run java class without main() method?

No, you can’t run java class without main method

 

What is the requirement to declare main() method as public?

🡪In Java applications,JVM has to access main() method inorder to start application

execution.

🡪 To access main() method by JVM first main() scope must be available to JVM.In

this context,to bring main() method scope to JVM,we must declare main() method

as “public”.

Case-1:

If main() method is declared as “private” then main() method will be available upto the main class,not to JVM.

 

Case-2:

If main() method is declared as “default” then main() method will be available upto the package where main class is existed,not to the JVM.

 

Case-3:

If main() method is declared as “protected” then main() method will be available upto the package where main class is existed and upto child classes available other packages but not to the JVM.

Case-4:

To make available main() method JVM,only one possibility we have to take that is “public”,public members will be available through out our system,so that,JVM can access main() method to start application execution.

 

What is the requirement to declare main() method as “static”?

Ans:In Java applications,to start application execution JVM has to access main()

method.JVM was implemented in such a way that to access main() method by using

the respective main class directly.

In Java applications,only static methods are eligible to access by using their  respective class name,so that as per the JVM predefined implementation we must declare main() method as static.

 

Related Posts: 

Commonly asked programs in java interview

Most commonly asked Constructor interview questions in

Main Method Interview Question in java

Inheritance Interview Question in java

TOP 200+ JAVA Interview Questions And Answers

Interview Questions And Answers

Angular Interview Questions and Answers

[TOP 50] JUNIT Interview Questions And Answers

Happy Learning !!

All the best.