Results 1 to 3 of 3
  1. #1
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default A query about main method

    I understand that main method is the point, from where the program execution starts. Makes a lot of sense. If I have a package, can that package have 2 classes with main methods defined? If yes, from where the execution will start as there are 2 execution points now. Please briefly explain.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    If I have a package, can that package have 2 classes with main methods defined?
    Yes. You can put/have a main method in any class. This is a way to allow testing of a class.
    If yes, from where the execution will start as there are 2 execution points now.
    The jvm looks for a class in a file with the public modifier and a main method for/at startup. The name of the source file, the (exact same) name of the class in the file that has the public modifier and that has a main method is the starting point. This classes main method will be the starting point.
    In packages you usually have each class in a separate file. If a file has multiple outer classes then you may have only one outer class with the public modifier in a file.
    In java, let's say I have a class "SkyScraper" and run it with "java ScyScraper" at the prompt. Then the jvm looks in the SkyScraper.class file for
    Java Code:
    public class SkyScraper {
        ...
        /** The jvm looks for this as the starting point. */
        public void main(String[] args) {
            ...
        }
    }
    
    class WindowWasher {
    
    }

  3. #3
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

Similar Threads

  1. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 01:37 PM
  2. Main method with throws Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 01-07-2008, 02:48 PM
  3. Private main method
    By bugger in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 09:45 AM
  4. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 09:25 AM
  5. The main method in java...
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 06:21 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
  •