How to have two main methods in a java program ?

Discussion in 'Java' started by aks_java, Jun 10, 2009.

  1. aks_java

    aks_java Guest

    Hi, I've built a GUI calculator and now my professor is asking the
    following:

    Prove the separation by having two “main” programs for the
    calculator, one that calls the GUI version of the calculator, one that
    calls a command line version of the calculator. The command line
    version should have an interface that shows the current display
    between square brackets, then a prompt (like the “>” sign) then awaits
    your input. So a sample session should look something like :

    [0.] > 78

    [78.] > -

    [78.] > 5

    [5.] > =

    [73.] > quit

    Now here's my problem: How do I've two main methods in a single java
    package ? I've looked around the internet to find examples of this
    but I could find none that actually worked. Netbeans gives errors
    straight away. I tried this for example:

    class Checkmain{
    public static void main(String args[]){
    args[1]="ashish";
    System.out.println("hello ");
    }
    }
    class Checkmain1 extends Checkmain{
    public static void main(String args[]){
    System.out.println("how r u");
    }

    }
    class Jo{
    public static void main(String args[]){
    String S[]=new String[10] ;

    Checkmain.main(S);
    Checkmain1.main(S);
    }}

    Now, I wonder if it is even possible to do this. How come no java book
    talks about it ?
     
    aks_java, Jun 10, 2009
    #1
    1. Advertising

  2. On 2009-06-10, aks_java <> wrote:
    > Now here's my problem: How do I've two main methods in a single java
    > package ?


    Put them in separate files. In your example, put Checkmain in
    Checkmain.java, Checkmain1 in Checkmain1.java and Jo in Jo.java.

    Cheers,
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Jun 10, 2009
    #2
    1. Advertising

  3. aks_java

    aks_java Guest

    On Jun 10, 8:48 am, Bent C Dalager <> wrote:
    > On 2009-06-10, aks_java <> wrote:
    >
    > > Now here's my problem: How do I've two main methods in a single java
    > > package ?

    >
    > Put them in separate files. In your example, put Checkmain in
    > Checkmain.java, Checkmain1 in Checkmain1.java and Jo in Jo.java.
    >


    Thanks, now it works. Is this the same as overriding the main method ?
     
    aks_java, Jun 10, 2009
    #3
  4. On 2009-06-10, aks_java <> wrote:
    > On Jun 10, 8:48 am, Bent C Dalager <> wrote:
    >> On 2009-06-10, aks_java <> wrote:
    >>
    >> > Now here's my problem: How do I've two main methods in a single java
    >> > package ?

    >>
    >> Put them in separate files. In your example, put Checkmain in
    >> Checkmain.java, Checkmain1 in Checkmain1.java and Jo in Jo.java.
    >>

    >
    > Thanks, now it works. Is this the same as overriding the main method ?


    No, the main method is static so you can't override it.

    When you run the program you need to choose which class gets to run
    and it is the main method in this class that will be executed. If you
    say you want to run the Checkmain class, then Checkmain's main() will
    be run and if you say you want to run the Checkmain1 class, then
    Checkmain1's main() will be run.

    That Checkmain1 is derived from Checkmain is not significant in this
    case since everything happens in a static context.

    Cheers,
    Bent D
    --
    Bent Dalager - - http://www.pvv.org/~bcd
    powered by emacs
     
    Bent C Dalager, Jun 10, 2009
    #4
  5. aks_java

    Mayeul Guest

    aks_java wrote:
    > On Jun 10, 8:48 am, Bent C Dalager <> wrote:
    >> On 2009-06-10, aks_java <> wrote:
    >>
    >>> Now here's my problem: How do I've two main methods in a single java
    >>> package ?

    >> Put them in separate files. In your example, put Checkmain in
    >> Checkmain.java, Checkmain1 in Checkmain1.java and Jo in Jo.java.
    >>

    >
    > Thanks, now it works. Is this the same as overriding the main method ?


    No, it's different. The main method being static, there is little point
    in overriding it anyway.

    (Actually, I'm not even sure defining a new main method in a subclass
    would be called overriding.)

    --
    Mayeul
     
    Mayeul, Jun 10, 2009
    #5
  6. aks_java

    Lew Guest

    Mayeul wrote:
    > No, it's different. The main method being static, there is little point
    > in overriding it anyway.
    >
    > (Actually, I'm not even sure defining a new main method in a subclass
    > would be called overriding.)


    It isn't. It's called "hiding". JLS, s. 8.4.8.2.

    Overriding is a very specific thing, allowing subclass (instance)
    methods to be invoked through a supertype reference.

    --
    Lew
     
    Lew, Jun 10, 2009
    #6
  7. RedGrittyBrick, Jun 10, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    544
    Andrew Thompson
    Oct 31, 2005
  2. Replies:
    5
    Views:
    320
    James Kanze
    May 24, 2007
  3. Keith Halligan

    Re: Do i have to have a main function?

    Keith Halligan, May 23, 2007, in forum: C++
    Replies:
    3
    Views:
    274
    James Kanze
    May 24, 2007
  4. mike-yue
    Replies:
    20
    Views:
    2,133
    Walter Roberson
    Apr 6, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    377
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page