What is Main Method in Java? And Usage

What is Main Method in Java? And Usage
The Java main method is a method of a Java program that is always executed at the beginning.

It is because a Java program can be executed as long as it has a main method.

In this post, I will tell you about the main method.

What is the main method?

The Java program has a method that is executed at the beginning. It is called the main method, and every program starts from this method. 

Let's look at the following sample program.

"Hellow World" on the console, which is often seen in introductory Java books and websites for beginners, but this program shows that it is written in the main method. 

Let's remember that a Java program starts with the main method, whether it is a short program like this or a program that consists of many classes. 

What happens if there is no main method?

What happens if your program does not have a main method? Let's look at the following sample program.

Instead of main, it is named start. In the case of this program, it compiles successfully, but when it is executed, it returns the following error. 

"java.lang.NoSuchMethodError:main" means An error has occurred because there is no method named main. 

Thus, in a Java program, the main method is the program's entrance and execution, so it is necessary to prepare the main method correctly.

Format of the main method

The main method is set up in the following format.

The "public" is an access modifier, meaning the public scope of the program; the access modifier of the main method must be public. Note that if "public" is omitted or another access modifier is specified, the program cannot be executed. 

The details of "static" will be explained at another time, but a "static" class can be executed without special declarations.

What does "args[]" in the argument of the main method mean?

This can be seen by executing the Java program on the command line. T

he command is as follows when executing the sample program "SampleClass" from the command line.

$ java SampleClass

Usually, this is how the program is executed, but it can also be executed by passing arguments, as shown below. 

$ java SampleClass "SampleArg1" "SampleArg2"

In this case, "SampleArg1" and "SampleArg2" are passed as arguments when SampleClass is executed, and these arguments correspond to the "args[]" argument of the main method.

In some cases, such as when linking from an external program, the arguments are specified for execution, and this is used in such cases, so it is essential to keep this in mind.

Summary

I have provided fundamental knowledge about Java's main method in this post.

It is the knowledge that you must know when you touch Java. I hope you will find it helpful.

Java Book for Beginner

The most important selling points of Head First Java is its simplicity and super-effective real-life analogies that pertain to the Java programming concepts. 

It is also the best book to learn java and start your learning journey with Java Development. 

Head First Java covers almost all OOPS concepts and fascinatingly explains them. 

Despite several readers claiming it to be a dated book, as it covers nothing beyond Java 5.0, Head First Java is still found on the shelves of numerous Java veterans.

Thus, it is a must-have book for every Java pursuer and developer.