Java Main() Method

Java Main() Method

In this article we will be discussing about Java Main() Method .

In Java, the main method is a special method that serves as the entry point for a Java application. When you run a Java program, the Java Virtual Machine (JVM) starts executing the code from the main method. The main method in Java is typically one of the first concepts you encounter when beginning to learn Java programming. It serves as the starting point for running a Java program.

Main Method in Java

What is main Method?

The main() Function is the starting point for the Java Virtual Machine (JVM) to start executing a Java program. Without the main() method, the Java Virtual Machine will not start the program.

The syntax of the main() method is:
Method in Java
Syntax:

public static void main(String[] args){

Explanation:

Public:

“Public” is the access modifier of the main method. It must be public so that the Java runtime can call this method. Note that if you make any method non-public, no program will be able to invoke it, So it means that the main method must be public.

Static:

There is no class object present when the Java runtime starts. Therefore, the main method must be static so that the Java Virtual Machine can load the class into memory and call the main method. If the main method is not static, the Java Virtual Machine will not be able to call it because there is no pre-defined class object present in the java virtual machine.

Void:

Void is a return type which specifies the return type of the function whether it will be integer or it will be of any other type. if the function isn’t returning anything the return of the function will be void. Java programming requires that every method provide a return type. Java’s main method does not return anything, so its return type is void. This was done to keep things simple, because once the main method is finished, the Java program exits.

Main:

This is the name of main method and It’s already written and fixed in the Java Virtual Machine. when we start a java program, The Java Virtual Machine looks for the main method.

String[] args:

It accepts a single argument of type String array. This is also called as java command line arguments.you can change args  to myStringargs. Also String array argument can be written as String… args or String args[] but you can’t change String type to any other variable type like int or float.

 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Example:

Run
import java.util.*;

public class Main{
    public static void main(String[] args){        
        // Code
    }
}

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription