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

Programming java in Programming 3 years ago

  1   0   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

The public static void main(String ARGS[]) is the main method of the Java Program. It is the entry point of any Java Program. A Java program cannot be executed without the main method.

The syntax of the main method is as follows:

public static void main(String args[])

Only the args can be changed to a different name but the rest of the method carries the same syntax.

Let us analyze the main method by breaking it up:

  • public: This is the access specifier/access modifier of the main method. It should be public to allow the class to access it. In case of violation of the same, the program will SHOW an error.
  • static: At the start of the runtime, the class is devoid of any object. Thus, the main method needs to be static so that the Java Virtual Machine can set the class to the MEMORY and CALL the main method. If the main method is not static then the JVM won’t be able to call it due to the absence of an object.
  • void: It is the return type of the main method. Each and every method in Java must have a return type. The main method doesn’t return any value that can be accessed by the other methods as the main method can’t be called in any other method. As soon as the main method finishes its execution the program terminates. Hence it wouldn’t be of any use to return a value.
  • main: It is the name of the main method. This name is ALREADY set and can’t be changed. If any changes are made to the main keyword then the compiler tells that the main method couldn’t be found.
  • String args[] :  The main method can accept a single argument which is of the String type. They are also called command line arguments. They can be used and manipulated as normal arguments in the code written in the main method. They are written along with the execution statement.

Posted on 31 May 2022, this text provides information on Programming related to java in Programming. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Tuteehub forum answer Answers

Post Answer

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.