Results 1 to 7 of 7
  1. #1
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Question [SOLVED] Mulitple main methods

    I am preparing for a test on java tommorow and came across this question. I just wanted to ask why it only goes through the normal "main(String args[])" method instead of the other one??

    10. What will happen if you compile/run the following code?
    Java Code:
    1: public class Q11
    2: {
    3: static String str1 = "main method with String[] args";
    4: static String str2 = "main method with int[] args";
    5:
    6: public static void main(String[] args)
    7: {
    8: System.out.println(str1);
    9: }
    10:
    11: public static void main(int[] args)
    12: {
    13: System.out.println(str2);
    14: }
    15: }
    A) Duplicate method main(), compilation error at line 6.
    B) Duplicate method main(), compilation error at line 11.
    C) Prints "main method with main String[] args".
    D) Prints "main method with main int[] args".

  2. #2
    imaginationac is offline Member
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default

    Why don't you just try it out to see yourself?

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    The answer is essentially "because it does". The VM always looks for a method with the exact following signature:

    Java Code:
    public static void main(String[] args)
    It isn't enough just to call the method "main": it must take precisely the right arguments (an array of Strings).

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    To expand on what Neil said, a method can be "overloaded". That means the same method name can be defined with different parameters. This is a *feature* of Java, and of all Object Oriented languages. The parameter types, taken together, are called the method's "signature".

    public static void main(String[] anyFieldNameAtAll) {}

    is a special method called the "entry method". A class with an entry method must be specified when the JVM is started, so the JVM knows what code to begin executing.

  5. #5
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    imaginationac :
    Why don't you just try it out to see yourself?
    How about you learn to read and understand questions first before replying???

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Doh! I have to admit, the original post did imply the the OP knew the answer; they wanted to know "why"...

  7. #7
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    If this is solved, please use Thread Tools -> Mark As Solved so everyone knows.
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Replies: 5
    Last Post: 10-06-2008, 01:11 AM
  3. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 10:25 AM
  4. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  5. Replies: 1
    Last Post: 08-06-2007, 10: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
  •