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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Javanaut

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

Post 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:

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Javanaut

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

Post 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.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 6 guests