×

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

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic...

3 minutes read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

String Manipulation in Java

String Manipulation in Java In Java, string manipulation is a common task performed by programmer. Java String class provides many built-in functions that are used to manipulate string. The manipulation of...

4 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Conditional operator in Java

In Java, there are around eight operators, and among them, three operators are used to evaluate the condition and decide the Result based on the Result of the evaluated condition. Below...

4 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Various Operation on Queue using Linked List in Java

We keep track of front and rear pointers in a queue data structure. The head of the line points to the first item, and the back to the last. Rear is...

3 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.