Discussion in 'Strings, I/O, Formatting, Parsing' started by jilmca786, Oct 16, 2010.

  1. jilmca786

    jilmca786 New Member

    why every program starts from main()?
  2. ihdubus

    ihdubus New Member

    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.
  3. srikkanth

    srikkanth New Member

    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
  4. ihdubus

    ihdubus New Member

    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();

  5. durgesh891

    durgesh891 New Member

  6. arunaramzz

    arunaramzz New Member


    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.
  7. arunaramzz

    arunaramzz New Member

    Am sorry did u run this program. Without Main function jvm couldn't run any class.
  8. ihdubus

    ihdubus New Member

    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

  9. abhijeku

    abhijeku New Member

    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...
  10. Mui

    Mui New Member

    Hai Friends,
    instead of using public static void main(String[] args) {
    we can use static to execute the Program


Share This Page