How to pass variable number of argumants to "method"(not main())
hi
i am devloping a program in which i have to pass variable number of arguments
to the method defined in other class how would i do that
for example,
public class myClass{
public static void main(){
OtherClass oClass = new OtherClass();
/**
i want exectly the same implemantation
as shown below
*/
oClass.otherMethod("arg1","arg2","arg3"); //all the arguments
oClass.otherMethod("arg4"); //are String
}//!myClass
//this class is defined in other file and dosnt have
// main() method
public class OtherClass{
//what should i put as the arguments the otherMethod takes
public otherMethod(?????????){....implemantation...}
} //!OtherClass
Re: How to pass variable number of argumants to "method"(not main())
manish <mbd72@hotmail.com> wrote in message news:38db7cd1@news.devx.com...
>
> hi
> i am devloping a program in which i have to pass variable number of
arguments
> to the method defined in other class how would i do that
> for example,
> public class myClass{
> public static void main(){
> OtherClass oClass = new OtherClass();
> /**
> i want exectly the same implemantation
> as shown below
> */
> oClass.otherMethod("arg1","arg2","arg3"); file://all the arguments
> oClass.otherMethod("arg4"); file://are String
> }//!myClass
>
> file://this class is defined in other file and dosnt have
> // main() method
>
> public class OtherClass{
>
> file://what should i put as the arguments the otherMethod takes
> public otherMethod(?????????){....implemantation...}
>
> } file://!OtherClass
>
>
public class OtherClass {
public void otherMethod(String a, String b, String c)
{...code for 3 arguments here...};
public void otherMethod(String a)
{...code for 1 argument here...};
}
will solve your problem if you only want to pass 1 or 3 strings as in the
example. If you want to pass any number of strings, pass an array of
strings.
public class OtherClass {
public void otherMethod(String[] a)
{...code for n arguments here...};
}
Re: How to pass variable number of argumants to "method"(not main())
hi
the first ex dosent work because i dont know the arguments
it might be 1,2,3,...so on.
in the second code i have to pass the arg. as as shown below
oClass.otherMethod(new String []{"arg1","arg2","arg3"});
but i dont want to do that i want to pass the arguments as
oClass.otherMethod("arg1","arg2","arg3");
is it possible
thanks for your help
manish
"Paul Clapham" <pclapham@core-mark.com> wrote:
>
>manish <mbd72@hotmail.com> wrote in message news:38db7cd1@news.devx.com...
>>
>> hi
>> i am devloping a program in which i have to pass variable number of
>arguments
>> to the method defined in other class how would i do that
>> for example,
>> public class myClass{
>> public static void main(){
>> OtherClass oClass = new OtherClass();
>> /**
>> i want exectly the same implemantation
>> as shown below
>> */
>> oClass.otherMethod("arg1","arg2","arg3"); file://all the arguments
>> oClass.otherMethod("arg4"); file://are String
>> }//!myClass
>>
>> file://this class is defined in other file and dosnt have
>> // main() method
>>
>> public class OtherClass{
>>
>> file://what should i put as the arguments the otherMethod takes
>> public otherMethod(?????????){....implemantation...}
>>
>> } file://!OtherClass
>>
>>
>
>public class OtherClass {
> public void otherMethod(String a, String b, String c)
> {...code for 3 arguments here...};
> public void otherMethod(String a)
> {...code for 1 argument here...};
>}
>
>will solve your problem if you only want to pass 1 or 3 strings as in the
>example. If you want to pass any number of strings, pass an array of
>strings.
>
>public class OtherClass {
> public void otherMethod(String[] a)
> {...code for n arguments here...};
>}
>
>
>