Hunt for India's Best Academic Project Contest - 2015

Important Links To Visit: 


Click: List of Shortlisted Participants | Click: Round II Details (Important) 


Can Java program run without the Main method?

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

  1. Sada

    Sada Star

    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

    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 java compiler needs to use this main method without creating any object of that class.

    please correct me if i am wrong.
  3. 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

    @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

    Thanks a ton guys...!

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

    Sada Star

    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" );
  7. :confused::confused::confused::confused:
    have you execute it running successfully
    • Like Like x 1
  8. sarveshgupta

    sarveshgupta Certified CEan

    @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

    The java program can run without main method with this static method
  10. sarveshgupta

    sarveshgupta Certified CEan

    But where is the function name in this program.

    With which name you will save the file?
  11. Yeah i am totally confused with this...where is the name of function
    as static is the reserved can you use it...
    if this is possible.
    then what would we say it,,,,it's feature or limitation??
  12. sookie

    sookie Addict

    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:\"

     * @author sookie
    public class ProgramWithoutMainMethod {
        System.out.println("Look at me ! I am running without Main method");      
    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:\

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

    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. thanks sookie ........for clarifying this concept
  14. ms_cs

    ms_cs Apprentice

    sookie: you are right..nice explanation
  15. accoolaryan

    accoolaryan Certified CEan

    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
    public Nomain{
    System.out.println("printing without main");
  16. rama_krish627

    rama_krish627 Apprentice

    107627@ccc108:~> javac 'class' or 'interface' expected
    public void static( )
    ^ <identifier> expected
    public class
    ^ '{' 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

    ya nice explanation sookie
  18. sookie

    sookie Addict

    Pardon my ignorance but I couldn't see where is your program ?

    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
    System.out.println( "Without main method" );

Share This Page