Hi Parveen

Each method has its "signature" based on:
-- the name of the method
        and
-- the number and the types of the parameters.

Java allows you to have two (or more) methods with the same name, but with a different list of parameters. When calling such a method by name, Java looks in the passed parameters and identifies the proper function to call (in fact, everything is already fixed by the compiler itself).

The course example tries to illustrate this feature. You are allowed to have two "main" methods as long as they have different parameters (one accepts an array of String, the other one accepts an integer).

When you type on the command line "java TwoMains", it looks for a static method named "main" and accepting an array of String as parameter in order to execute it. It will call no other static method called "main" but with other parameters.

Attention: the difference is made based on the list of parameters and not on the returned value. Thus Java will not allow you to name two methods with the same name, having for both the same parameters, but having different return types (the compiler would return an error).

Hope it helps
Mihai

Le 13/04/2011 07:47, Parveen Thakur a écrit :
HI,

The following code is run well but how ??Because i Right 2 main method in single class pl z explain this in brief ,



public class TwoMains
{
/** This class has two main methods with
* different signatures */
public static void main (String args[])
{
//required prototype for main method
System.out.println("Hello world!");
int i;
i = main(2);
System.out.println ("i= " + i );
}
/**This is the additional main method*/
public static int main(int i)
{
return i*i;
}
}

Thanks in advance.

--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com To unsubscribe from this group, send email to javaprogrammingwithpassion+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en

--
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to