Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1055 :

Posted: Tue Sep 25, 2012 7:31 pm
by Javanaut
-The explanation for option 4 says that, "The ... syntax is not applicable for return type". My question is how does one specify a varargs return type? Do I just put an array is the return type or is there another way to do this?

:shock: :geek:

Re: About Question enthuware.ocajp.i.v7.2.1055 :

Posted: Tue Sep 25, 2012 8:25 pm
by admin
You can't have var args as return type. A method can return only one thing. Otherwise, how will you assign the return value ? Something like :

Object retvalue = methodX(); //if methodX returns multiple value, which value will you assign to retvalue?

If you want to return multiple things, you can return an array or a collection. For example,
Object[] relvalue = methodX();//methodX returns one object i.e. one value, which happens to be an array.

ArrayList relvalue = methodX();//methodX returns one ArrayList object, which may contain multiple objects.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1055 :

Posted: Tue Sep 25, 2012 10:38 pm
by Javanaut
Hi Paul,

Thanks for the explanation. I guess that it is almost impossible to return a varargs to something.

A primitive variable obviously can not take more than one value. It seems like an ArrayList could though since its size can expand unlike an Array, which is fixed.

I will just have to remember varargs is not a valid return type for Java.