• Dear Guest, Please do NOT forget to join our JAVA JOBS (ONLY) FACEBOOK GROUP to be aware of latest JAVA openings, and you may also post any JAVA related queries/jobs.


In Simple : Jvm needs a default method to start program and from that method it starts instantiating the objects of other classes. so if every one uses their own methods then jvm is not able to find from where it need to start compiling. so Java people kept a default method not main() method, it is argumented main ie "main(String var[])" and this shoubld be always public and static. so that it is visible to the jvm.
I think ur question is Y, a java program cotains a method main() that takes String args[] as parameter i,e main(String args[]).

When we compile a java program, then java compiler comes into picture and it compiles the java program from top to bottom with out checking main method. i,e for just compiling a java program, main method is not required. But if we want to run a java program JVM comes into picture. Here JVM searches for a "public static void main(String args[])" method for executing the program. JVM blindly executes the program from the "public static void main(String args[])" method. So without "public static void main(String args[])" JVM doesnot executes the java program. After calling main method only all other tasks will be done.. such as object creations... etc.. So "public static void main(String args[]) " method is must in a java program for running.

Why public --> Because the program should available to JVM (JVM is also a program)
Why static --> Because without creating an object JVM should execute main method
Why void --> Because main methos doesnot return any value.

For more information contact me on srikkanth.99999@gmail.com
it is not compulsory to write public static void main(String[] s){
with out main also we can run.

public class Demo{
System.out.println("In Demo Constructor");

static {
Demo d=new Demo();

why every program starts from main()?

Every program starts with the main function because that is how the run-time library startup routines work. It is the established convention. There has to be some entry point, otherwise the program could not do anything, and main() is the defined entry point.

That is, main() is the entry point after run-time library startup.
Not the program starts with main, only its execution. In the source the main can be at the very end of the file.
Dear arunaramzz...

please run the program from command prompt.

i got the following output when i run from command prompt

C:\>java Demo

In Demo Constructor

What you did was , you called the default constructor from a static block
which gets initialized before main , so ur programme runs
but this can't be done on every case on commmercial scale...