File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Qn from Javacert

 
Praveen Zala
Ranch Hand
Posts: 118
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: }
What is the o/p ?
Why is main() still compiled though there is a duplicate
main() method ?
Praveen Zala
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Adrian Yan
Ranch Hand
Posts: 688
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic