×

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

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Parallel Arrays Sort in Java

Sorting is a technique of arranging a sequence of numbers in ascending order, that is, the first number being lowest and gradually incrementing the numbers such that the last number...

7 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.