Java main method location

Java main() Method – public static void main(String[] args)

In Java programs, the point from where the program starts its execution or simply the entry point of Java programs is the main() method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very important.

The Java compiler or JVM looks for the main method when it starts executing a Java program. The signature of the main method needs to be in a specific way for the JVM to recognize that method as its entry point. If we change the signature of the method, the program compiles but does not execute.

Читайте также:  Php date after tomorrow

The execution of the Java program, the java.exe is called. The Java.exe in turn makes Java Native Interface or JNI calls, and they load the JVM. The java.exe parses the command line, generates a new String array, and invokes the main() method. A daemon thread is attached to the main method, and this thread gets destroyed only when the Java program stops execution.

Java main() Method

Syntax of main() Method

The most common in defining the main() method is shown in the below example.

Java

Every word in the public static void main statement has got a meaning in the JVM that is described below:

1. Public

It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.

Java

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

2. Static

It is a keyword that is when associated with a method, making it a class-related method. The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.

Java

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

3. Void

It is a keyword and is used to specify that a method doesn’t return anything. As the main() method doesn’t return anything, its return type is void. As soon as the main() method terminates, the java program terminates too. Hence, it doesn’t make any sense to return from the main() method as JVM can’t do anything with the return value of it.

Java

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

4. main

It is the name of the Java main method. It is the identifier that the JVM looks for as the starting point of the java program. It’s not a keyword.

Java

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

5. String[] args

It stores Java command-line arguments and is an array of type java.lang.String class. Here, the name of the String array is args but it is not fixed and the user can use any name in place of it.

Java

Apart from the above-mentioned signature of main, you could use public static void main(String args[]) or public static void main(String… args) to call the main function in Java. The main method is called if its formal parameter matches that of an array of Strings.

FAQs on Java main() Method

Q1. Can the main method be int? If not, why?

Java

prg1.java:6: error: missing return statement > ^ 1 error

Java does not return int implicitly, even if we declare the return type of main as int. We will get a compile-time error

Java

Error: Main method must return a value of type void in class GeeksforGeeks, please define the main method as: public static void main(String[] args)

Now, even if we do return 0 or an integer explicitly ourselves, from int main. We get a run time error.

Explanation of the above programs:

The C and C++ programs which return int from main are processes of Operating System. The int value returned from main in C and C++ is exit code or exit status. The exit code of the C or C++ program illustrates, why the program was terminated. Exit code 0 means successful termination. However, the non-zero exit status indicates an error.

For Example exit code 1 depicts Miscellaneous errors, such as “divide by zero”.

The parent process of any child process keeps waiting for the exit status of the child. And after receiving the exit status of the child, cleans up the child process from the process table and free the resources allocated to it. This is why it becomes mandatory for C and C++ programs(which are processes of OS) to pass their exit status from the main explicitly or implicitly.

However, the Java program runs as a ‘main thread’ in JVM. The Java program is not even a process of Operating System directly. There is no direct interaction between the Java program and Operating System. There is no direct allocation of resources to the Java program directly, or the Java program does not occupy any place in the process table. Whom should it return an exit status to, then? This is why the main method of Java is designed not to return an int or exit status.

But JVM is a process of an operating system, and JVM can be terminated with a certain exit status. With help of java.lang.Runtime.exit(int status) or System.exit(int status).

Q2. Can we execute a Java program without the main method?

Yes, we can execute a Java program without a main method by using a static block.

A static block in Java is a group of statements that gets executed only once when the class is loaded into the memory by ClassLoader, It is also known as a static initialization block, and it goes into the stack memory.

Q3. Can we declare the main() method without String[] args?

Yes, we can declare the main() method without String[] args. Although it will generate an error message if we directly try to execute the main method inside the driver class as done in the below example.

Источник

Main Method in Java | public static void main(String[] args)

Scientech Easy

A main() method in java is an entry point to start the execution of a program. Every Java application has at least one class and at least one main method.

Normally, an application consists of many classes and only one of the class needs to have a main method.

In simple words, a complex program can have dozens of classes but only one of the classes needs to have a main() method to get things started. Therefore, java main() method is the starting place of your program.

The syntax for declaration of the java main method is as follows:

Syntax: public static void main(String[] args) < // Method body goes here. >

main method in java

In the above declaration, two modifiers such as public, and static has been used with the main method. Let’s see a brief explanation and purpose of each of the terms used in the main method.

1. public : The public modifier makes it accessible from anywhere in the application.

2. static : The static modifier makes it a class method so that it can be called using the class name without creating an object of the class.

3. void : The return type of the main method is void which means that it does not return a value to its caller. You must specify void when you declare the main method.

4. main : It is the name of a method where execution will start. In Java, the main method is called by JVM.

5. String[ ] args : The main method accepts one argument of type String array (String[ ]). The square brackets [ ] represent the array of strings that is passed as an argument to this method.

args is the name of its parameter. You can use any parameter name as you wish.

For example, you can declare the main method like this public static void main(String[] myParameter), which is the same as declaring the main method as shown previously.

7. main Method Body : The region between the opening brace and closing brace is called main method body that contains a set of program statements. This region is a static region.

8. ( > ) : This is a closing brace that marks the closing of the main method body. It must be paired with an opening brace.

Why public static void main in Java?

Basically, the public static void main(String [ ] args) acts as an entry point to start the execution of Java application program. That’s why we use/write public static void main in java program.

Why do we need to declare main method as static in Java?

The main method is declared as static. It is called by JVM when we run a class. The JVM does not know how to create an object of a class. It needs a standard way to start the execution of a program.

Therefore, the main method is declared as static so that the JVM can call it using the class name which is passed on the command line.

Why main method is declared as static in Java?

If we do not declare the main method as static, it will be considered as an instance method. The code will be compiled successfully without generating any error message. But at runtime, the code will generate an exception named: “NoSuchmethodError: main”.

How to call main method in Java?

The main method is called by JVM when we run a class.

Can we have more than one main() method in class?

Yes, a class can have any number of main() methods but the execution always starts from public static void main(String[ ] args) only. Let’s take an example program where we will declare more than one method.

Program source code 1:

public class MainTest < public static void main(int a) < System.out.println("Second main() method"); main(); >public static void main() < System.out.println("Third main method"); >public static void main(String[] args) < System.out.println("main(String[] args)"); main(20); >>
Output: main(String[] args) Second main() method Third main method

In the above example program, we have declared three main() methods. The first main() method is declared as public static void main(String[] args) used as an entry point to run the program.

As far as JVM is concerned, the other two main() methods have no special significance. They have been declared only to print the message on the console.

Can we execute a program without main() method in Java?

Yes, we can execute a program without main() method in Java in the previous version of JDK. One of the ways is a static block. But from onwards JDK 1.7 and above, is not possible.

If the main() method has no argument of array reference of string type, the program source code will be compiled successfully without generating any error but at runtime, the program will terminate by generating an exception named: “NoSuchMethodError: main”.

Can we overload main method in java?

Yes, we can overload the main() method but we cannot override it. We can declare any number of main() method in a class, but the method signature must be different. Let’s make a program where we will overload the main method.

Program source code 2:

class OverloadMainMethod < public static void main(int a) // Overloaded main method < System.out.println(a); >public static void main(String args[]) < System.out.println("main method invoked"); main(10); >>
Output: main method invoked 10

1. public static void main(String args[ ]) is a line at which the program will start executing.

2. main() method in java is always called by JVM (Java Virtual Machine) before any objects are created.

3. It does not return any value.

4. Since Java is case-sensitive, Main is different from main. If you type Main instead of main, java compiler would still compile your program but java will report an error because it would not find main() method.

5. If a program does not contain the main method in a class, Java compiler will compile it but cannot run it.

6. Java main() method can be overloaded but cannot override it.

Источник

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