UnicMinds

why public static void main is used in Java

The Meaning of public static void main(String [] args) in Java

The main method in every Java program which is written as follows, but what does it mean?

public static void main (String [] args)

The method should be public. If you don’t specify the method as public, then the compiler will throw an error.

Static should be used because when the main method is called we don’t have any instantiated object and hence the main method should belong to the class and not the instantiated objects. To enable this process, we use the keyword “static”. 

Void shows that the method is not returning any value.

The method name has to be “main”. If we give some other name, then the JVM cannot identify the main method to start execution. When we compile a .java file, it gets converted into a .class file and this .class file is given to the JVM. Each OS will have its own JVM but the .class file (with bytecodes) is platform independent. So, the JVM reads the .class file and converts it to machine language code for execution.

JDK = JRE + JVM + Development Tools;

JRE = JVM + Library Classes

JVM = Class Loader + JVM Memory + Execution Engine

String [] args: args is an array type of String. The main method accepts the command line arguments of string data type.  args is just a variable name, and you can give any other name you like too.

Hope this is useful, thank you.

You may like to read: Programming Tic-Tac-Toe in Java, Block Coding Courses for Kids, & Introducing Python Programming to Kids

BOOK A FREE TRIAL