This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
(1) Code will not compile and will give "Duplicate main() method declaration" error (2) Code will compile correctly but will give a runtime exception (3) Code will compile correctly and will print "In first main()" (without quotes) when run with argument of 'a'. (4) Code will compile correctly and will print "a" (without quotes) when run with argument of 'a'. SKIP THE QUESTION Their answer: 3 Why will this run with argument of 'a'? [ Jess added UBB [code] tags to preserve whitespace, check 'em out! ] [ January 06, 2003: Message edited by: Jessica Sant ]
I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
when you call this program: java MyClass any arguments can go here the JVM looks for a method who's signature looks like this: public static void main( String ) and that's the method that it will call. the fact that you have overloaded the main() method to have another signature as well is inconsiquential to the JVM: public static void main( char ) is a totally different method -- and the JVM won't call it by itself. You can call it if you want to... but the JVM will only call the one who's arguments is String The fact that you started the program with the argument 'a' won't affect which method is used either -- in fact you can call it with however many agruments you like... they'll each be stored in that String array [ January 06, 2003: Message edited by: Jessica Sant ]
Executing a Java program isn't exactly like invoking a function within a Java program. There are a couple things to keep in mind. First of all, when you invoke a Java program, the JVM looks into the class you want to run for a method with the following signature:
If it finds that method, it executes it, passing any parameters you've supplied in the String array. If it does not find that method, it will issue an error. The JVM will never execute the method:
automatically. That doesn't mean you couldn't invoke it yourself, explicitly, but that's not what the JVM will do when you execute the program. It doesn't check to see what types of paramaters you've provided, it just interprets them all as strings. Even had you provided a number such as 2, it still would have considered that a String. I hope that helps, but you can check out the JLS, §Execution for more details. Corey