Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Esoteric question regarding main function

  1. #1
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Esoteric question regarding main function

    I began coding my application a while ago. Added 2 classes and then added a main() function to test it. It ran without posting any of the test output lines I had sprinkled through, so I started with the debugger and discovered I had 2 main() functions. One I had added and another empty one that was autogenerated by the IDE. Deleted the extra main() fuinction, but my question is:

    Why didn't this obvious error generate a compiler error? I understand that a class can have multiple functions with the same name and different arguements, but is there a condition when one would need 2 program entry points?
    Last edited by ras_oscar; 09-16-2013 at 03:28 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,354
    Rep Power
    6

    Default Re: Esoteric question regarding main function

    The main() methods must have been in different classes, or else you would have gotten a duplicate method error.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Esoteric question regarding main function

    A program USUALLY would not NEED "2 entry points", but there is there any reason that it CAN'T? (Other than, of course, that you can't have 2 syntactically correct main methods in the same class.)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,025
    Rep Power
    6

    Default Re: Esoteric question regarding main function

    You can have multiple classes and each one may have a main entry point. Which one gets used as the entry point to run the app depends on which class is specified on the command line. And of course, the other classes may call different main entry points since they are just static methods.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ras_oscar is offline Member
    Join Date
    Jun 2013
    Posts
    60
    Rep Power
    0

    Default Re: Esoteric question regarding main function

    hmmmm interesting. Yes, the main() functions were in different classes. So that means I could write an entry point for public use and a separate entry point for debugging. I'll have to think about that.

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default Re: Esoteric question regarding main function

    There are plenty of reasons to have multiple entry points.. maybe one of them starts up a gui, whereas the other is a command-line program. One is for debugging, the other for production.

    As others have said, there's nothing stopping you (nor should there be) from having different classes with main methods.

    This is also why I say that beginners shouldn't use IDEs. You were confused because your IDE was hiding what was going on from you. Had you been using the command line, it would be obvious what was going on.
    quad64bit likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 1
    Last Post: 01-10-2012, 01:49 PM
  2. Random Function Question.
    By Dark in forum New To Java
    Replies: 13
    Last Post: 04-06-2011, 05:51 AM
  3. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  4. Rewrite as a function so it can call from main.
    By thangli in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 07:26 AM
  5. how to add main function
    By spratana in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •