This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes var args constructors/methods vs lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "var args constructors/methods vs lists" Watch "var args constructors/methods vs lists" New topic
Author

var args constructors/methods vs lists

Sergio Castro
Greenhorn

Joined: Feb 10, 2013
Posts: 2
In the long debate of using arrays vs lists in the public API of Java classes, I tend to prefer lists in most situations.
However, I also found convenient to use var arguments in many constructors and methods, which require declaring an argument of the method as an array (implicitly adding "..." before the parameter name).
Since in many of these methods I do want to provide a programmer the possibility to either explicitly list the arguments (with var args) or use a pre-defined collection when invoking the method, I often end up defining a constructor or method with var arguments, and an overloaded version that accepts a list instead of the var args array. For example:



I know that in theory I do not need to overload the method replacing the var args array with a list if I want the programmer to send a collection of objects. He could just send an array.
The reason I often do this is because most (if not all) of the methods in my API use lists instead of arrays, and it is a bit weird that for one or two methods requirying collection of objects, the programmer needs to work with arrays (in cases when the var args are not convenient).
So I provide an overloaded version working with list for consistency.

However, since this is a recurrent problem, I have the uncomfortable feeling of being writing boilerplate code too often. I would like to receive feedback if my motivation of keeping my interface consistent (accepting var arrays or lists, but not forcing the programmer to work with arrays if not really required) is valid enough to keep writing overloaded versions using lists for most of my methods requirying var arguments.
I want to highlight that I know that for certain methods arrays could be more convenient than lists. My question concerns situations where the only motivation for declaring the last argument of the method as an array is because in certain scenarios using var arguments can be convinient.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

If you want to use Lists, there are two ways you can still use the benefits of varargs:

1) Create the varargs constructor / method but make it call the List one, using Arrays.asList:

2) Create only the List version and use Arrays.asList whenever you want to use varargs.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sergio Castro
Greenhorn

Joined: Feb 10, 2013
Posts: 2
Rob Spoor wrote:If you want to use Lists, there are two ways you can still use the benefits of varargs:

1) Create the varargs constructor / method but make it call the List one, using Arrays.asList:

2) Create only the List version and use Arrays.asList whenever you want to use varargs.


This is what I do right now, I just clarified the question. Thanks !
This is rather a design question where I am wondering if the work of adding an overloaded method accepting a list is worthy the effort just to keep consistency. This has been bothering me since quite some time ago, given that it is a quite recurrent situation I face.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: var args constructors/methods vs lists
 
Similar Threads
Not just another Primitive Data Type Question ? Why Priimitive ? Varargs
varargs, overloading and widening
Tiger objectives - take 2
what is int... refer to?
Sun Cirtification