×

Why main method is static in Java

The method serves as the entry point for Java programmes or simply the point from which the programme begins to run. As a result, it is one of the most crucial Java methods, making it essential to comprehend it thoroughly.

When a Java application is being run, the JVM or Java compiler searches for the primary method. The main method's signature must be in a specified format for the JVM to recognise it as the entry point. The software builds but does not run if we alter the method's signature.

The java.exe programme is used to run the Java programme. The JVM is then loaded by Java.exe, making JNI calls. Invoking the primary () method after parsing the command line and creating a new String array. When the Java programme stops running, the daemon thread tied to the main function is removed.

Syntax: Typically used when defining the main() method

A sample java program for Hello world in java.

class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Output:

Hello, World!

Explanation of output: The public static void main statement's words are all notifed by the JVM.

  1. public
  2. static
  3. void
  4. main

public: It is an access modifier that specifies who can access the method and where they can do so. The main() method becomes globally accessible when it is made public. Since it is not a part of the current class, it is made public so that JVM can call it from outside the class.

// Java programme illustrating the
// use of any additional access modifier
// exclude the public
class HelloWorld {
private static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Output:

Error: A javafx application class must extend javafx.application to prevent the main method from failing to comply with public static void main's criteria (String[] args). Application

Static:

When used in conjunction with a method, it is a keyword that designates the technique as class-related. The static nature of the main() method allows JVM to call it without first creating the class. Additionally, this prevents the needless waste of memory that would have been incurred had the object been defined only to have the JVM invoke the main() method.

// Java application to show the
// When we don't use them, an error happens.
// The main() method uses the static keyword.
class HelloWorld {
private static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Explanation of output: The main() method in the class test should be public static void main instead of not being static (String[] args)

void:

A keyword is employed to indicate that a method returns nothing. The main() method's return type is void because it doesn't produce a result. The main() method's termination also causes the Java programme to end. As a result, returning from the main() method is useless because the JVM cannot use its return value.

class HelloWorld {
private static void main(String[] args) {
System.out.println("Hello, World!");
return 1;
}
}

Explanation of output: Either a JavaFX application class must extend or the main function must be a public static void main(String[] args). javafx.application. The main form is not found in the category. Application

Main:

The Java main method has that name. When the Java programme is first run, the JVM searches for this identifier. It’s Not at all a keyword.

class HelloWorld {
public static void myMain(String[] args) {
System.out.println("Hello, World!");
}
}

Explanation of output:

In the event that the main method cannot be found in the category, the JavaFX application class must extend javafx.application. The main method might also be made public static void main as an alternative (String[] args). Application

String[]args:

It contains Java command-line arguments and is an array of type java.lang. Class for strings Although args is the String array's name. The user is free to substitute any other name in this case.

Numbers.java

class Numbers
{
public static void main(String[] args)
{
for (String elem : args)
System.out.println(elem);
}
}

Output:

5
6
7

In addition to the above-mentioned main function signature, java also supports calling the main function with public static void main(String args[]) and public static void main(String args). If the formal parameter of the main method matches an array of Strings, the main method is invoked.

The justifications for making the main() function static:

  1. Users already know that when the JVM starts, there are no objects of a class. Because of this, we are unable to call a method without first generating an instance of its class. The main() method is made static to allow the JVM to load the class into main memory.
  2. The entry point of any Java programme is the main() function. The main() method is necessary since this is where the compiler initiates programme execution.
    3.If it is allowed for the main() function to be non-static, the JVM must instantiate the class.
  3. By using the class name alone, JVM can easily use the static methods without generating an instance of the class.
  4. As already mentioned, the main() method needs to be static, public, and void return. If the method doesn't return anything or if we don't specify it as public and static, it will surely throw an error.

Related Topics

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.

How to Convert String to Object in Java

How to Convert String to Object in Java The Object is the super class of all classes. So you can assign a string to Object directly. There are two methods to...

3 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

Get year from date in Java

The getYear() method of the Java date class returns a number calculated by deducting 1900 from the year that contains or starts with the instant in time represented by this...

4 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Reserved Keywords in Java

In Java, a reserved term is used as a code key called a keyword. Because they are predefined, these terms cannot be used for anything else. They cannot serve as...

3 minutes read.

Merge Sort in Java

Merge Sort in Java Merge sort in Java uses the divide and conquer approach to sort the given array/ list. There are three steps involved in the merge sort. 1) Divide the...

5 minutes read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Uses of Java

Java is used in many real-world Java applications, including technologies and tools. This Java programming language has become the backbone for developing many applications. In areas like embedded systems and...

3 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.