Results 1 to 7 of 7

Thread: Without main

  1. #1
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Without main

    Hey guys,

    I have a doubt. I read somewhere that a java program without main method may run!!!. Is that true??? if yes then how????

    Thank you.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    No and yes. The program that is started by the java command must have the main() method. I don't know a way to tell the java program to use another static method.
    If you write an 'interface program' with a main, that program can create an instance of another class and optionally call any method in that class.

    Another type of program not needing a main is an applet. In that case instead of being required to have a main, it is required to have an init method.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    For an example,

    Java Code:
    class NoMainMethod
    {
        static
        {
            System.out.println("this is a text");
            System.exit(0);
            
        }
    }

  4. #4
    maya700 is offline Member
    Join Date
    Jun 2010
    Posts
    45
    Rep Power
    0

    Default Thanks

    Thank you.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Thanks. I learned two new things yesterday.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  2. Replies: 1
    Last Post: 07-16-2009, 10:35 AM
  3. Replies: 5
    Last Post: 10-06-2008, 12:11 AM
  4. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  5. Replies: 1
    Last Post: 08-06-2007, 09:11 AM

Posting Permissions

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