Click to See Complete Forum and Search --> : Main method ??
jayarani115
07-14-2004, 07:33 AM
Hi,
I am new to Java programming. Please clarify me the following doubts.
Why does the main method accept a String array as parameter?
Can the main method accept an int array as parameter?
Can the return type of main method be int or double or anything else? If so, where does it return?
Thanks in advance.
mikeBarr81
07-14-2004, 04:02 PM
The main method signature cannot be anything other than public static void main(String[] args). It can't return anything as it must be void, and it can't accept an int array or any other array but a String array. The string array is provided to it by the jvm and consists of a list of parameters provided at the command line by the user.
java MyProgram param1 param2 param3The above line entered on the command line would start MyProgram, and the main method would be supplied with a string array of length 3. Index 0 has param1, index 1 has param 2 and index 3 has param 3. You can use it to supply any information you want to the program, such as filenames or numbers etc.
jayarani115
07-15-2004, 11:07 AM
Ok, fine. If the signature of the main method is that way, I'll proceed with the same. But, where is the main method declared? ie, where can I find the signature of main method? Can I use,
javap java.<package>.class
If so, in which package and class?
Drain
07-15-2004, 11:32 AM
Um, it is declared in whatever classes use a main method. What exactly are you asking?
mikeBarr81
07-15-2004, 04:04 PM
any class can declare and use a main method. You write it yourself, and the jvm uses it to start your program. It is the very first part of your code that will be run when you start your application.
devx.com
Copyright Internet.com Inc. All Rights Reserved