Cyber Man wrote:Then why did the code compiled in the first plaace?
There is nothing wrong with having a main() method that takes a string -- as it can be called by other parts of your program. However, if you want that main() method to be the starting main() method of the java application, the signature is not correct.
I. Haq wrote:Then why did the code compiled in the first plaace?
Because main method in your example is an ordinary method you can have as many as such methods in your program but when you run your program using java command then JVM looks for a special main method having following signature declared with public static modifiers in the class whose name you mention whilst running the program.
This special main method is an entry point of the program. Above program compiles successfully, when you run above program using command java Test here JVM searches for special main method and executes It where It prints "I'm special main method" then invokes other main method having String as parameter which prints "Hi I'm an ordinary main method"
If you don't have such special main method then program can be compiled successfully ( If syntactically everythng is correct ) but when you run using java command then It will give runtime exception saying
Error: Main method not found in class Test, please define the main method as: public static void main(String args)