1. Become a Developer for CrazyEngineers - Apply NOW!

Can Java program run without the Main method?

Discussion in 'Computer Science | IT | Networking' started by Sada, Nov 17, 2009.

  1. Sada

    Sada Star

    Engineering Discipline:
    Computer Science
    My friend asked me this question.!

    Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?

    Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..

    CAn anyone tell me the reason?
    And correct me If am wrong?
     
  2. faizaan

    faizaan Certified CEan

    Engineering Discipline:
    IT
    Re: Do a Java program runs without the Main method?

    My point of view is that,

    If we create an applet in java than it can run without having main method,but if we develop an application than it do requires main method.

    Reason why we require main method is because we write public static void main() i.e main method is a static method & so we can call it without creating object of that class . so when we compile (i.e javac filename.java) java compiler needs to use this main method without creating any object of that class.

    please correct me if i am wrong.
     
  3. Manish Goyal

    Manish Goyal Ace

    Engineering Discipline:
    Computer Science
    I think main() method is must in java ...
    since java is pure object oriented language....and also main() method is an entry point...through which execution of program begins............
     
    • Like Like x 1
  4. sarveshgupta

    sarveshgupta Certified CEan

    Engineering Discipline:
    Computer Science
    @pallavi: Faizan is right it is possible to have a program to start execution without entering into main first.

    This is because main is always the entry gate for the program. but if we are able to provide any other initiation point there is no need of main

    as in case of an applet in which there is no main method, it has init
     
  5. Sada

    Sada Star

    Engineering Discipline:
    Computer Science
    Thanks a ton guys...!

    So, main method is must to run a java program...!
     
  6. Sada

    Sada Star

    Engineering Discipline:
    Computer Science
    guys..! My friend told me that we can write and run a java program without using the main method.
    i.e By using static

    she gave this example program...!

    public void static( )
    public class
    {
    public void static( )
    {
    System.out.println( "Without main method" );
    System.exit(0);
    }
    }
     
  7. Manish Goyal

    Manish Goyal Ace

    Engineering Discipline:
    Computer Science
    :confused::confused::confused::confused:
    have you execute it...is it running successfully
     
    • Like Like x 1
  8. sarveshgupta

    sarveshgupta Certified CEan

    Engineering Discipline:
    Computer Science
    @Pallavi: try the code yourself as I am doubtful this program will run.

    And I told you that Java Applet programs don't generally have the main method.

    they have paint() and init() method.
     
  9. ms_cs

    ms_cs Apprentice

    Engineering Discipline:
    Computer Science
    The java program can run without main method with this static method
     
  10. sarveshgupta

    sarveshgupta Certified CEan

    Engineering Discipline:
    Computer Science
    But where is the function name in this program.

    With which name you will save the file?
     
  11. Manish Goyal

    Manish Goyal Ace

    Engineering Discipline:
    Computer Science
    Yeah i am totally confused with this...where is the name of function
    as static is the reserved keyword..how can you use it...
    if this is possible.
    then what would we say it,,,,it's feature or limitation??
     
  12. sookie

    sookie Addict

    Engineering Discipline:
    Computer Science
    If I am not wrong ms_cs mean to say by using 'static block' not 'static method' because for calling a static method, one need to have an entry point while 'static block' is automatically called whenever class is loaded in JVM. "Static blocks" declared in any Java class are always called before the main() method. You can also note that why static variables or methods are declared outside "main" method.

    Let's see an example to avoid any confusions
    STEP 1: Create a class like followng in any of the locations of your sytem. Let me keep it "I:\"

    Code:
    /**
     *
     * @author sookie
     */
    public class ProgramWithoutMainMethod {
     static
    {
        System.out.println("Look at me ! I am running without Main method");      
        System.exit(0);
        
    }
    }
    
    STEP 2: Now open command prompt and go to"bin" directory of your installed JDK and enter following command. It compiles the program
    javac I:\ProgramWithoutMainMethod.java
    [​IMG]

    STEP 3: Now let's simply run the program.
    java -classpath I:\ ProgramWithoutMainMethod
    [​IMG]

    Now you all might be wondering that why do I need to add this additional line "System.exit(0); " right? See, as soon as my "static block" reaches its closing brace, it tries to search for main() method which as of now you all now is must for running a Java program so in order to stop my program from looking for main() method, I would simply call System.exit(0); and terminate the entire Java program. So I am not giving it a chance to look for any main() method.

    Hoping now you all might be cleared about the things discussing over here.

    PS: Before directly asking questions [specially related to Java] - A tip try to search web, it is having so much information that you will learn 100 other things with only 1 question. :)
     
    • Like Like x 2
  13. Manish Goyal

    Manish Goyal Ace

    Engineering Discipline:
    Computer Science
    thanks sookie ........for clarifying this concept
     
  14. ms_cs

    ms_cs Apprentice

    Engineering Discipline:
    Computer Science
    sookie: you are right..nice explanation
     
  15. accoolaryan

    accoolaryan Certified CEan

    Engineering Discipline:
    Computer Science
    yes we can run a java program without a main method .as main method is necessary but if we use a block named static ,every statement in it will be executed without main
    try it
    but it can give a n error so use System.exit(0) at the end of the file
    as
    public Nomain{
    static{
    System.out.println("printing without main");
    }
    System.exit(0);
    }
     
  16. rama_krish627

    rama_krish627 Apprentice

    107627@ccc108:~> javac crazy1.java
    crazy1.java:1: 'class' or 'interface' expected
    public void static( )
    ^
    crazy1.java:2: <identifier> expected
    public class
    ^
    crazy1.java:9: '{' expected
    }
    ^
    3 errors

    These are the errors in this program.
    can you tell me is it working properly?
    If yes tell me how.
     
  17. sarveshgupta

    sarveshgupta Certified CEan

    Engineering Discipline:
    Computer Science
    ya nice explanation sookie
     
  18. sookie

    sookie Addict

    Engineering Discipline:
    Computer Science
    Pardon my ignorance but I couldn't see where is your program crazy1.java ?

    I am just assuming
    You are using "class" keyword in your program and name of the file and class marked as public are same.
     
  19. techno_ishant

    techno_ishant Certified CEan

    java program can run without main()
    For Example APPLET...:happy:
     
  20. rahulgakhar

    rahulgakhar Certified CEan

    ya u can run a java program without main

    try this

    public class A
    {
    static
    {
    System.out.println( "Without main method" );
    System.exit(0);
    }
    }
     

Share This Page