psvm in Java

Disha Hati
3 min readApr 17, 2022

--

psvm in Java stands for public static void main(String[] args).

It is the most important Java method. When we start learning Java(or any other coding language),the first function(method in Java) we encounter is the main() method. It is the point from where the program starts its execution.

public

The first word ,i.e. p is public which is the access modifier of the main method. The “public” keyword means that any object can use the main method. We can access the class from anywhere. The main() has to be public as, if it is non-public, then it’s not allowed to be executed by any program.

Let’s see what happens if we don’t define main() as public.

static

The second word, i.e. s is static which allows main to be called without having to instantiate any object. When java runtime starts, there is no object of the class present. If the main method won’t be static, JVM would not be able to call it because there is no object of the class is present. We could also write static public instead of public static, it would work the same, but public static is the standard way.

Let’s see what happens when we don’t use static in java main() method.

void

The third word, i.e. v is void which is the return type of the main method. The return type “void” specify that method doesn’t have a return value. This is done because once the main() terminates, java program also terminates. There is nothing that can be done for the returned object by JVM. So there is no point in returning anything.

main

The fourth word, i.e. m is main which specifies that it is the entry point of the program. It’s fixed and when we start a java program, it is the main() that the JVM looks for as the starting point of the java program. Remember, it is an identifier, not a keyword.

String[] args

The java main() accepts a single argument of array of type java.lang.String class. This is also called as java command line arguments. Everything in the psvm statement is fixed except this part. You can change the name of String array argument, for example you can change args to myStringArgs. String array argument can also be written as String... args or String args[].

Behind the Scene

When the java program starts executing, the java.exe is called. The java.exe makes JNI(Java Native Interface) calls, and this load JVM. The java.exe parse the command line, generates a new string array, and invokes the main() method. A daemon thread is attached to the main method, and this thread gets destroyed only when the Java program stops execution.

How can C/C++ program return int , but not Java?

While coding in C/C++, we may or may not return value. The C and C++ programs which return int from main are processes of Operating System. The value returned from main is the exit code or exit status, which mainly specifies why the program was terminated. Exit code 0 means successful termination while non-zero exit code indicates an error.

However, the Java program runs as a ‘main thread’ in JVM. The Java program is not even a process of Operating System directly. There is no direct interaction between the Java program and Operating System. There is no direct allocation of resources to the Java program directly, or the Java program does not occupy any place in the process table. Then whom should it return the value to?

This is the reason why the main method of Java is designed not to return an int or exit status.

--

--