aspose file tools*
The moose likes Beginning Java and the fly likes return multiple values from a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return multiple values from a method" Watch "return multiple values from a method" New topic
Author

return multiple values from a method

Phoebe Song
Ranch Hand

Joined: Jan 17, 2002
Posts: 54
Hi,
What's the correct syntax for returning multiple values from a method?
for example I want a method return int x and int y. Is this correct?

public int amethod
{
some codes here


return x, y;
}

Thanks for any help.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
You cannot return multiple values from a method. If you have more than one value you want returned, you could encapsulate them into an object and return a reference to the object.
Dana Bothner-By
Ranch Hand

Joined: Jul 28, 2006
Posts: 37

If you want to return x and y values, it sounds like a point in space to Muggins.
Phoebe Song
Ranch Hand

Joined: Jan 17, 2002
Posts: 54
I see... Thank you.
Prasanna Chennupati
Ranch Hand

Joined: Feb 09, 2006
Posts: 38
If you want to return multiple values from a method,wrap those values in an array or return them as a collection.

Here is an example

public List someMethod()
{

//do some manipulation.
List list = new ArrayList();
list.add(x);
list.add(y);
//add more values as you need.
return list;
}

when you want to return multiple values,returning them in the form of collection classes is a good idea.You might use Array too.


Thanks,<br />Prasanna<br />SCJP1.4,SCWCD1.4,SCBCD1.3,SCDJWS,SCEA-1<br />IBM Certified Solution Developer -- WebSphere Portal V5.1(got 100%)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Or write more than one method, each of which returns one of the values.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608


Tony Morris
Java Q&A (FAQ, Trivia)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Or put the code that uses the values you get back into the class you got them from. If you follow the multiple gets with a calculation and a set ... well, there's your sign.

Could maybe be


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return multiple values from a method