Running main method java

Java main() method explained with examples

In this article, we will learn Java main() method in detail. As the name suggest this is the main point of the program, without the main() method the program won’t execute.

What is a main() method in Java?

The main() method is the starting point of the program. JVM starts the execution of program starting from the main() method.

Syntax of main() method:

public static void main(String args[])

public: We have already learned in the access specifier tutorial that public access specifier allows the access of the method outside the program, since we want the JVM to identify the main method and start the execution from it, we want it to be marked “public”. If we use other access modifier like private, default or protected, the JVM wouldn’t recognise the main() method and the program won’t start the execution.

Читайте также:  PHP Form

static: The reason the main() method is marked static so that it can be invoked by JVM without the need of creating an object. In order to invoke the normal method, we need to create the object first. However, to invoke the static method we don’t need an object. Learn more about static method here.

void: This is the return type. The void means that the main() method will not return anything.

main(): This the default signature which is predefined by JVM. When we try to execute a program, the JVM first identifies the main() method and starts the execution from it. As stated above, the name of this method suggests that it is the “main” part of the program.

String args[]: The main method can also accepts string inputs that can be provided at the runtime. These string inputs are also known as command line arguments. These strings inputs are stored in the array args[] of String type.

Can we have main() method defined without String args[] parameter?

If we have a main() method without String args[] in a program, the program will throw no compilation error however we won’t be able to run the program as the JVM looks for the public main method with the String args[] parameter and if it doesn’t find such method, it doesn’t run the program.

Error: Main method not found in class JavaExample, please define the main method as: public static void main(String[] args)

As you can see that the program threw error at runtime.

Java – static block vs main method

As we learned in the previous article, static block is used to initialise the static data members. Let’s run a program with static block and main method (static method) to see in which order they run.

class JavaExample < //static block static < System.out.println("Static Block"); >//static method public static void main(String args[]) < System.out.println("Main Method"); >>

As we can see, the static block executed before the main method.

What if a program doesn’t have a main method?

Let’s write a program without the main method to see whether it runs or not.

Error: Main method not found in class JavaExample, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Different ways to write main method in java

The following are the valid ways to write a main method in java:

public static void main(String[] args) //We can interchange static and public static public void main(String[] args) //We can place the square brackets at the different locations public static void main(String[] args) public static void main(String []args)

Overloading of main() method in Java

We can overload the main method in Java. This allows us to have more than one main() method in Java. However the signature of all the overloaded methods must be different. To learn more about overloading, refer this guide: Method overloading in Java.

class JavaExample < public static void main(String args[]) < System.out.println("main method"); main(100); main('A'); >//Overloaded int main method public static void main(int a) < System.out.println(a); >//Overloaded char main method public static void main(char ch) < System.out.println(ch); >>

Frequently Asked Questions

The main method is used to specify the starting point of the program. This is the starting point of our program from where the JVM starts execution of the program.

No, you can’t declare a method inside main() method.

Yes we have can more than one main methods in java, however JVM will always calls String[] argument main() method. Other main() methods will act as a Overloaded method. In order to invoke these overloaded methods, we have to call them explicitly.

No, we cannot override main method of java because it is a static method and we cannot override a static method. The static method in java is associated with class which is why we don’t need an object to call these. Therefore, it is not possible to override the main method in java.

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Java main method Explained [Easy Examples]

The java main method is a standard method that is used by JVM to start the execution of any Java program. The java main method is referred to as the entry point of the Java application. In this tutorial, we will learn about the java main method. We will see how we can create the java main method and explain its every component in detail.

We will also discuss how we can pass different arguments to the java main methods by taking various examples. Moreover, we will also learn the overloading of the java main method. All in all, this is going to be one of the most informative tutorials about the java main method.

Getting started with Java main method

As we already discussed the java main method is the entry point for any Java application. The main method will subsequently invoke all the other methods required by the program. In this section, we will discuss the syntax and different ways of writing the java main method and will explain each of its components in more detail.

Different Java main method syntax

The java main method can be written in many ways depending on the situation. Although they’re not very commonly used, they’re valid signatures. Note that none of those are specific to the main method, they can be used with any Java method but they are also a valid part of the main method.

Syntax-1 to use java main method

Let us see some of those methods here. See the simple syntax of writing the java main method.

public static void main(String[] args) < // statements >

Syntax-2 to use java main method

The above one is the most commonly used syntax of the java main method. Notice that the square brackets are just after the String but they can also be written after args as well. See the syntax below:

public static void main(String args[]) < // statements >

This is a valid declaration of the java main method.

Syntax-3 to use java main method

Another way of writing the main method is to add strictfp with it, which is used for compatibility between processors when working with floating-point values. See the simple syntax below:

public static strictfp void main(String[] args) < // statements >

Syntax-4 to use java main method

Another way to write the java main method is to write the final inside the main method which can be applied on args to prevent the array from being modified. See the simple syntax below:

public static void main(final String[] args) < // statements >

Notice that the above all are valid but not commonly used methods of declaration, the very first one is the most popular one and we will also use that syntax in the rest of our tutorial.

Different ways of using Java main method

Understanding the “public” keyword

The public is a Java keyword that declares a member’s access as public. Public members are visible to all other classes. This means that any other class can access a public field or method. Further, other classes can modify public fields unless the field is declared as final. The main method in java is public so that it can be accessible everywhere and to every object which may desire to use it for launching the application.

Notice that if we will not make the main method public, then there will be no compilation error. But we will get runtime error because matching the main method is not present. Remember that the whole syntax should match to execute the java main method. For example, see the example below:

java main method

Notice that it says the main.java is not executable, that is because our main method is not public.

Understanding the keyword “static”

In the Java programming language, the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type. This means that only one instance of that static member is created which is shared across all instances of the class. In simple words, static methods are the method that invokes without creating the objects, so we do not need any object to call the main method. If we will not make the main method static we will get the following error. See the example below:

java main method error

Notice that it says the main method is not found and gave us the proper syntax of the main method.

Understanding the keyword “void”

The void keyword specifies that a method should not have a return value. In java, every method should have a return type otherwise we will get an error. The main method’s type is void which means that it is not returning anything. If we create the main method without any return type (void) we will get the following error.

java main method void

Understanding the “string arg[]” in the main method

The java main method can also accept some data from the user. It accepts a group of strings, which is called a string array. It is used to hold the command line arguments in the form of string values. The agrs[] is actually an array name, and it is of String type. It means that it can store a group of strings. Remember, this array can also store a group of numbers but in the form of a string only. Values passed to the main method are called arguments. These arguments are stored into args[] array, so the name args[] is generally used for it.

Later in this tutorial, we will take examples and see how we can pass arguments to the main method.

Simple example of Java main method

Now we are familiar with the terms used in the java main method, we are good to go and create a main method and run it. See the example below which prints out a message.

Источник

Оцените статью