• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to return multiple values from methods

 
Anil Kumardvg
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to return multiple values from method. How can we return different data type values from one method.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Why not? But not through different/multiple return statements at a time

You can very well make use of Collections. You can use any collection say ArrayList, HashMap etc to incorporate more than one value. But they typically deal with Objects.

In case of returning the primitive values via this collection, you have to go for the corresponding Wrapper Classes.

HtH.
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to return more than one value from any method is to use an object.

If any of the parameters to your method are objects and the instance variables are mutable, you can change these fields -- but you can't change the object-reference variables that are passed in as parameters.

Arrays are objects as are Collections and any other object that is mutable can have more than one of its instance variables changed by your method.

You can only return only one value in a return statement, but since this returned value can be an object, you can return an object that constists of more than one value by creating an object in your method and then returning the object that your method has created.

Kaydell
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is to use an Object[] as the return type.

Or you can create a class that contains fields for all the return values.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats great kaydell. I missed out this point..


The only way to return more than one value from any method is to use an object


The object can be of anything. Collections are all objects.
[ May 23, 2007: Message edited by: Raghavan Muthu ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd avoid collections and Object[] solutions as they hide the meaning of the method. I'd rather create a custom object just to hold the two or three things the method might modify. For example, if you want to return x and y coordinates, int[] would be a bad return type, Point would be a good one.

If you can't find a descriptive name and make a cohesive object to return, you probably have some other design issues that ought to be ironed out, like maybe the method is doing too much.
 
Anil Kumardvg
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that you don't want to make up classes that are unnecessary, but I remember that a long time ago, I worked with a software engineer that choose to use the Point datatype in Classic MacOS, programming in Pascal.

Unlike the new Point datatype in Java, this old datatype of the same name used 16-bit signed integers and once it was stored on disk, we were stuck with a 32K limit.

We should have defined our own type, let's say called "cellID" for a spreadsheet-like object. Then, since we owned the type, we could have used 32-bit integers and had a limit of 4 billion rows.


It seems that even Microsoft made the same mistake as we did. They had a limit of 64K rows in Excel. That's a lot, but I know of a consultant that wanted to take a books-in-print database consisting of 3 million rows and import it in Excel and massage the data to be in a form to input into a database program.

With Excel's 64K limit, this meant that he'd have to reformat the data about 50 times instead of just once. He also had to figure out a way to break the data file into pieces.

I have three points, if you use an existing type, make sure that it really is going to be sufficient, and 2) if you ever store a type to disk, make sure that you're not "painting yourself into a corner", and 3) is you make up your own types, your code may be more readable, saving you time and money.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is to introduce a Method Object - a class that has only the method returning just one of the values (or even void), and getters for all the other values you need to retrieve.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic