*
The moose likes Beginning Java and the fly likes Is it possible to return multiple values from methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is it possible to return multiple values from methods" Watch "Is it possible to return multiple values from methods" New topic
Author

Is it possible to return multiple values from methods

Anil Kumardvg
Greenhorn

Joined: May 21, 2007
Posts: 15
Is it possible to return multiple values from method. How can we return different data type values from one method.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
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.


Ping & DNS - my free Android networking tools app
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Jan 29, 2003
Posts: 8791
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.


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
Anil Kumardvg
Greenhorn

Joined: May 21, 2007
Posts: 15
thanks alot
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
 
subject: Is it possible to return multiple values from methods