Results 1 to 7 of 7
Thread: [SOLVED] Mulitple main methods
- 02-09-2009, 04:20 PM #1
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
[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?
A) Duplicate method main(), compilation error at line 6.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: }
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".
- 02-09-2009, 08:45 PM #2
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Why don't you just try it out to see yourself?
- 02-09-2009, 09:22 PM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
The answer is essentially "because it does". The VM always looks for a method with the exact following signature:
It isn't enough just to call the method "main": it must take precisely the right arguments (an array of Strings).Java Code:public static void main(String[] args)
Neil Coffey
Javamex - Java tutorials and performance info
- 02-09-2009, 11:33 PM #4
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.
- 02-10-2009, 12:13 AM #5
Senior Member
- Join Date
- Jan 2009
- Posts
- 119
- Rep Power
- 0
imaginationac :
How about you learn to read and understand questions first before replying???Why don't you just try it out to see yourself?
- 02-10-2009, 12:16 AM #6
Doh! I have to admit, the original post did imply the the OP knew the answer; they wanted to know "why"...
- 02-10-2009, 01:49 AM #7
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
[SOLVED] Why main() in java is declared as public static void main?
By piyu.sha in forum New To JavaReplies: 5Last Post: 10-06-2008, 12:11 AM -
main method
By eva in forum New To JavaReplies: 5Last Post: 12-19-2007, 09:25 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM -
exception in thred main java.lang.nosuchmethoderror: main
By fernando in forum Java AppletsReplies: 1Last Post: 08-06-2007, 09:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks