File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Qn from Javacert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Qn from Javacert" Watch "Qn from Javacert" New topic

Qn from Javacert

Praveen Zala
Ranch Hand

Joined: Jul 02, 2000
Posts: 118
1: public class Q11
2: {
3: static String str1 = "main method with String[] args";
4: static String str2 = "main method with int[] args";
6: public static void main(String[] args)
7: {
8: System.out.println(str1);
9: }
11: public static void main(int[] args)
12: {
13: System.out.println(str2);
14: }
15: }
What is the o/p ?
Why is main() still compiled though there is a duplicate
main() method ?
Praveen Zala
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
Two methods with the same name and different parameters is method overloading. this is a feature of java.
main with String array as parameters as given in your query is the entry point for a java program. Hence it prints the string with array and not the one with int.
Amit Goel
Ranch Hand

Joined: Dec 07, 2000
Posts: 50
main() function can be defined two times as it defines method overloading which is a feature of java.
but as public static void main(String agrs[]) serves as the entry point for the program so it runs from this function and the other function serves as a simple function which is overloaded and can't give output till the time it is not called
through an object.

Amit<br /> <br />The Less I have, The more I gain..Off the Beaten Path, I Reign.
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
They are perfectly legal, because it's just overloading. If you run the program, the output is "main method with String[] args". Regardless of the arguments you pass in, since Java takes all command line arguments as String.
I agree. Here's the link:
subject: Qn from Javacert
It's not a secret anymore!