File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Not able to understand the rules of Var-args... please help... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not able to understand the rules of Var-args... please help..." Watch "Not able to understand the rules of Var-args... please help..." New topic
Author

Not able to understand the rules of Var-args... please help...

rizwana mujawar
Ranch Hand

Joined: Feb 24, 2012
Posts: 36
rule1)you must specify type of arguments that var-args parameter of your method can recieve
rule2)To declare a method using var-args parameter,you must follow the type with ellipsis(),space and then the name of the array that will hold the parameter recieved.
rule3)Its legal to have other parameters in a method that uses a var-args.
rule4)(This rule is confusing me the most). The var-arg must be last parameter in the methods signature,and you can have only one var-args in a method....
Can anyone explain me the 3rd and 4th rule...
void doStuff(char c,int...x) { } //this is legal
but then why below one is illegal..?
void doStuff(int ....y,char x) { }

void doStuff(String...s,byte b) { } //var-args must be last (I dint get)

Thanks...!
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

rule4)(This rule is confusing me the most). The var-arg must be last parameter in the methods signature,and you can have only one var-args in a method....


If you have a method that takes var-arg as a parameter then var-arg must be the last parameter.
suppose you have a method that takes 2 parameter one byte and one var-arg then you can't write as



because here your method accepting var-arg as a first parameter. You should write it as




rule1)you must specify type of arguments that var-args parameter of your method can recieve


as in above code you must have to written that ypur var-arg param type is String


Life is easy because we write the source code.....
rizwana mujawar
Ranch Hand

Joined: Feb 24, 2012
Posts: 36
Another doubt about var-args...
Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)
rizwana mujawar
Ranch Hand

Joined: Feb 24, 2012
Posts: 36
Thank you so much for those valuable replies. Thanks once again
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)


What you get when you tried? That three dots are compulsarry. Because doStuff(String.....s) and doStuff(String s) both are different methods.

doStuff(String s) => Means method is accepting only one string parameter.
But doStuff(String.....s)=> Means method can accept any number of String parameter.



Thank you so much for those valuable replies. Thanks once again


You are Welcome
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Pramod P Deore wrote:
Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)


What you get when you tried? That three dots are compulsarry. Because doStuff(String.....s) and doStuff(String s) both are different methods.

doStuff(String s) => Means method is accepting only one string parameter.
But doStuff(String.....s)=> Means method can accept any number of String parameter.


And the varargs one can also accept a String[] parameter.


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Pramod P Deore wrote:
Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)


What you get when you tried? That three dots are compulsarry. Because doStuff(String.....s) and doStuff(String s) both are different methods.


So, yes, you can write doStuff(String s), but then it's not a varargs method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to understand the rules of Var-args... please help...
 
Similar Threads
returning a value
Need Help
Advanced Overloading (widening,boxing,var args)
about var-args
var arg