×

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts the program running in java. For writing this method, we need to follow the syntax.

public static void main(String args[]);

From the syntax :

It contains different keywords. Each keyword has its value.

public keyword

The public is one of the access modifiers in java. Public keywords identify the JVM machine for the program's execution. This keyword is used for classes, methods, and constructors. Can use the keyword for accessing everywhere. We use public keywords to give identity for one or more classes in a program.

Example

class Main{
private static void main(String[] args)
{
System.out.println("I am a student");
}
}

Output

public static void main string args meaning in java

static keyword

A method can be static if it contains the static keyword. By using a static void, the main method can be called directly without creating an object, so by using this method, there is no need to create an object.

Example

class Main{
public void main(String[] args)
{
System.out.println("I am a student");
}
}

Output

public static void main string args meaning in java

void keyword

When compared to the c language, java has no return datatype. The void keyword implies that the compiler understands that it does not contain the return type in the main method.

Example

class Main{
    public static int main(String[] args)
    {
        System.out.println("JAVA");
        return 1;
    }
}

Output

public static void main string args meaning in java

main() method

Java Virtual Machine predefined the default signature. This method is called Java Virtual Machine for running the program for each line and executing the result. The main method can be overloaded.

String args[

The main() method is used for getting input from the user. The input given by the user treated like a String, called a string array. Holding up the values in the command line arguments in the form of string args[] is used. String args[] is the array name for storing string values, which means it can store strings. The args array can store numbers, but they are in string format. The values passed into the main() method are called arguments, which are used for storing input.

What results when the main() method doesn’t contain String args[]?

If the main method doesn’t contain string args[], the compiler will compile the program, but it doesn’t run. Due to the absence of string args[], JVM does not recognize the main method. JVM will recognize the main() method if it contains arguments passed in it.

Process of execution

At the initial state, the Java Virtual machine will run the code's static block as it is very easy to access the static memory area. After that, the methods declared in the static block are executed, and the JVM will create an object. At last, the instance methods will get executed. The static block in JVM is in top priority. Before execution of the main block, it checks in the static block.

Example

class Main
{  
static  //static keyword
{  
System.out.println("STATIC block");  
}  
public static void main(String args[]) // main method 
{  
System.out.println("Static methods");  
}  
}

Output

public static void main string args meaning in java

From the above program, we can understand that JVM will initially execute the static block in the program, and then it looks for the main() method. If the program doesn’t contain any main method, it returns an error that the main method is not found.

Program does not have main() method

class Main 
{  
static      //static method doesn't contain the main method
{  
System.out.println("Static block doesn't contain main method()");  
}  
}

Output

public static void main string args meaning in java

Hence the main method is very important in a program. The main() method can be interchanged in the following way:

static public void main(String args[])  

The main() method can use a different name in String as:

main(String[] x)

There are more two ways the writing the main() method:

  • static public void main(String []x)
  • static public void main(String…args)

The string argument is used to accept null or more values. For this, three dots needed to be included.

Overloading main() method

class Main  
{  
public static void main(int c)  //two same methods in a single program  
{  
System.out.println(c);  
}  
public static void main(String args[])  
{     
System.out.println("Main method get invoked");  
main(67);  
}  
} 

Output

public static void main string args meaning in java

Related Topics

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Static and Dynamic Binding in Java

Data Binding in Java The relation between a class and method or class and field is known as Data Binding. In Java, we can create a class for Student_info, but until we...

2 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 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.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

How to Iterate a HashMap in Java?

Hash-map is a class of Java collections framework which has the functionality to implement the Map interface of Java. It stores the data in key-value pairs and can be accessed...

5 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

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.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.