What is the correct syntax to declare a main method in Java?

Java MCQ: What is the correct syntax to declare a main method in Java?

a) public static int main(String[] args)
b) public static void main(String[] args)
c) public void main(String[] args)
d) static void main(String[] args)

Answer:

b) public static void main(String[] args)

Explanation:

The main method in Java is the entry point of any Java application. The correct syntax for the main method is public static void main(String[] args). This method must be declared as public so that it can be accessible to the JVM to execute the program.

The static keyword allows the JVM to call the method without needing to instantiate the class. The void return type indicates that the method does not return any value. Finally, String[] args is the parameter that allows the program to accept command-line arguments.

Understanding the main method’s syntax and purpose is fundamental for developing Java applications, as it is the first method that gets executed in a Java program.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top