Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Mapisto
  • 1 Post By Mapisto

Thread: main()

  1. #1
    ajansketab is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default main()

    hi all ,
    I want to know why each program need to have a main function , I mean that I dont know what does this function do that we must rewrite it ?
    for example in Android we don't have main function .


    thanks all . . .
    Last edited by DarrylBurke; 08-15-2012 at 04:55 PM. Reason: Removed siglink

  2. #2
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: main()

    I don't know about android programing yet, but the main method is where every program starts. from there you call all the other classes, methods, etc..

    Correct me if I'm wrong :)
    ajansketab likes this.

  3. #3
    ajansketab is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: main()

    thanks ,
    actually I want to know how main() function does this . how it can start .
    Last edited by DarrylBurke; 08-15-2012 at 04:56 PM. Reason: Removed siglink

  4. #4
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: main()

    Hmm...
    If I'd have to guess, I'd say that it has something to do with the way Java translated to Assembly.

    If you've got some experience with Assembly you'll probably know what I'm talking about, if not, I'll explain.

    In Assembly, the program starts with the basic order of lines.. from the first line to the second etc... like any other program.. UNTIL it reaches a "jump" or "branch" command that jumps to the specific row (tagged row) that was written in the jump/branch command.

    So, as far as I'm concerned, every language (except machine language ofc) gets translated to Assembly and therefore the compiler does the translating as it was programmed- in our case, it places a jump command somewhere in the start that jumps to the start of the main method.

    And again,
    Correct me if I'm wrong :)
    ajansketab likes this.

  5. #5
    ajansketab is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default Re: main()

    thanks a lot . . .
    you really helped . . .
    Last edited by DarrylBurke; 08-15-2012 at 04:56 PM. Reason: Removed siglink

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: main()

    When you launch the JVM and tell it the class to start with it needs something to call.
    That something is the main method.
    It needs to follow a standard signature since the JVM needs to be able to pass any parameters it is given on to the method itself.
    Since, for the command line, those parameters are Strings, then:
    public static void main(String[])
    is the obvious signature to define.

    Without it it would be rather complicated to know how to start it up.

    Android (like the Applet launcher in a browser) is already up and running and use entry points for their whatever screen you are loading.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    TrickyClimber is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Re: main()

    Also, to add to this, main() is a static method. Which means that it doesn't need to be instantiated to run the code in the method. This allows Java to have an entry point to start the program. The main method's purpose is to create the initial objects that make up the program.

Similar Threads

  1. Exception in thread main java.lang.nosuchmethoderror main
    By prikesh_joshi7 in forum Java Applets
    Replies: 3
    Last Post: 01-26-2012, 04:19 PM
  2. Replies: 1
    Last Post: 11-23-2011, 05:52 AM
  3. Replies: 1
    Last Post: 01-23-2011, 05:19 PM
  4. Replies: 5
    Last Post: 10-06-2008, 12:11 AM
  5. Replies: 1
    Last Post: 08-06-2007, 09:11 AM

Tags for this Thread

Posting Permissions

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