What is ‘Main’ method in Java!!

CodeAalayam
2 min readDec 10, 2023

--

Java is an object-oriented programming language. The life cycle of a java source code can be easily put as first your write the java code which is human readable(That’s how we can write:)) and using the compiler convert that source file to a machine readable code. Run the compiler output code on the JVM(Java virtual machine) platform. Tada!! The program is running now.

BUT! How JVM know where to start with the run?! Which object to make?! So confusing..ha!! Not at all, that’s where the Main method comes to rescue. You might have seen in the java application source code a method looks like this,

public static void main(String[] args){
//more code
}

This is the door for your application. JVM looks for this main method in the class and starts running from here. If you consider your java application as a home, this method ‘main’ is the entry. Let’s dissect otherwise what’s fun in it?? LOL!!

public: Ofcourse! if you keep it as a secret how will the JVM knows about it. ‘public’ signifies this method is visible outside

static: Static methods in java are assosicate with a class not an instance. So you don’t need to create an object to access a static method. Since the program execution starts from the main method, there’s no object existing at that point. Hence the method is static so that JVM can call it directlywith out creating an object.

main : The method name.

void: The return value of the method!! Hmm why so? Poor JVM doesn’t know what to do with the return value. If the main method finishes that means program is terminated.

String[] args : This helps to pass arguments to the program and accordingly we can write the logic..but not necessary always!!

That’s it!! Enjoy learning and coding…

--

--