A Question about two main method in one java file.
Gong James
Ranch Hand
Joined: Oct 15, 2001
Posts: 77
posted
0
What will happen if you compile/run the following 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".
in the class Q11 have two main method ,though they have different signater of args list.but I think the main method are special ,shoud have only one main method which declare "public static void".so I think the B are the right ans.But I compile and run the code ,the C is right ans.I am confused by the result. Could somebody kind enough give me a comment to remove my confusion,I will very graceful.
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Hi Gong, There is nothing special with the main method. just an agreement about it's signature to be called from java runtime. So it can be overloaded as any other method.
ersin eser
Ranch Hand
Joined: Feb 22, 2001
Posts: 1072
posted
0
it is an overloading and from command line all you can invoke is the string version C:\>jdk1.3.1_01\bin\java Q11 main method with String[] args C:\>jdk1.3.1_01\bin\java Q11 string main method with String[] args C:\>jdk1.3.1_01\bin\java Q11 12 main method with String[] args
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: A Question about two main method in one java file.