two main methods in a single program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ambikasd
    New Member
    • Sep 2007
    • 12

    #1

    two main methods in a single program?

    Hi,

    Please tell me whether we can have two main methods in a single java program?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by ambikasd
    Hi,

    Please tell me whether we can have two main methods in a single java program?
    [code=java]
    public class Main {
    private double main= 54.42;
    public Main() { }
    public static void main(String[] args) {
    Main main= new Main();
    main.main();
    main.main(42);
    main("can");
    }
    private void main() { System.out.prin t("yes "); }
    private void main(int i) { System.out.prin t("you "); }
    private static void main(String s) { System.out.prin tln(s); }
    }
    [/code]

    kind regards,

    Jos

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Originally posted by JosAH
      [code=java]
      public class Main {
      private double main= 54.42;
      public Main() { }
      public static void main(String[] args) {
      Main main= new Main();
      main.main();
      main.main(42);
      main("can");
      }
      private void main() { System.out.prin t("yes "); }
      private void main(int i) { System.out.prin t("you "); }
      private static void main(String s) { System.out.prin tln(s); }
      }
      [/code]

      kind regards,

      Jos
      Blech. That just looks awful.

      Comment

      • kreagan
        New Member
        • Aug 2007
        • 153

        #4
        Originally posted by JosAH
        [code=java]
        public class Main {
        private double main= 54.42;
        public Main() { }
        public static void main(String[] args) {
        Main main= new Main();
        main.main();
        main.main(42);
        main("can");
        }
        private void main() { System.out.prin t("yes "); }
        private void main(int i) { System.out.prin t("you "); }
        private static void main(String s) { System.out.prin tln(s); }
        }
        [/code]

        kind regards,

        Jos
        1.) Why would you want 2 main functions? Are you trying to implement multithreading or something?

        2.) Would you even consider that a main function: private static void main(String s) { System.out.prin tln(s); } ? You are just overloading the main function, nothing more. The difference between a real main function and that overloaded imposter is: the program points and knows to point at the real main function. ... Am I making sense?

        3.) That was really cute though.

        To answer the poster's question. If you mean a method where the program starts, how can a program start at 2 different places? My answer would be no.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by kreagan
          1.) Why would you want 2 main functions? Are you trying to implement multithreading or something?
          Huh? overloaded methods have nothing to do with multiple thread.

          Originally posted by kreagan
          2.) Would you even consider that a main function: private static void main(String s) { System.out.prin tln(s); } ? You are just overloading the main function, nothing more. The difference between a real main function and that overloaded imposter is: the program points and knows to point at the real main function. ... Am I making sense?
          Nope, but the OP wasn't making sense either ;-)

          Originally posted by kreagan
          3.) That was really cute though.
          I know ;-)

          Originally posted by kreagan
          To answer the poster's question. If you mean a method where the program starts, how can a program start at 2 different places? My answer would be no.
          Yup, and my terrible example just shows that. The question didn't make sense.
          Of course different classes can all have a static void main(String[] args) method.

          kind regards,

          Jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Ganon11
            Blech. That just looks awful.
            That Sir, is in the eye of the beholder ;-)

            kind regards,

            Jos

            Comment

            • kreagan
              New Member
              • Aug 2007
              • 153

              #7
              Originally posted by JosAH
              Huh? overloaded methods have nothing to do with multiple thread.
              Sorry, I was refering to the first poster's question, not your crazy program. To me, it sounds like he wants to run 2 programs (threads) with 1 code.

              Originally posted by JosAH
              Yup, and my terrible example just shows that. The question didn't make sense.
              Of course different classes can all have a static void main(String[] args) method.
              Right, but you can only run 1 at a time.

              Comment

              Working...