File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Returning Multiple Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning Multiple Variables" Watch "Returning Multiple Variables" New topic
Author

Returning Multiple Variables

Wally Hartshorn
Ranch Hand

Joined: Jan 30, 2003
Posts: 77
In Perl, I sometimes want to return multiple values from a routine. For example:

How would I do something similar in Java? I could just place the values in an array and return the array, except that the values aren't of the same type. I suppose I could make it an array of Strings and then convert the $total to numeric after it has been returned, but that feels a bit clunky.

Is there a more elegant way to do it?


Wally Hartshorn
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
i suppose you could wrap the things to be returned in wrapper classes (if they're primitives, that is), then cast them to Object, and return an array of Objects. it still seems clumsy, but i guess that's just static typing for you. :roll:
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

There are two ways to do this: the correct way, and the quick way.

Correct way:



Then you declare that your method returns this type. This is, after all, what OOP is about. This way, you caould return any number of variables; just add them to the class.

Alternatively, you could do this (and this is a cheat that should eventually be refactored into the above):



Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ben Wood
Ranch Hand

Joined: Aug 14, 2001
Posts: 342
Wally,

The way I see it is this, if you find yourself in a situation where a method needs to return more than one (primitive) value it suggests a missing object type. Sometimes this will be one you can use from the API, other times you will need to define a new class of your own.

From my own experience of learning Java I would suggest doing it to OO way rather than going for a quick fix which I used to do a lot before I got it. If you are new to Java, coming from something like Perl, then the practice doing it "the right way" will help you get your head round it much quicker IMO.


SCJP 1.4, www.gsi3d.org.uk
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'll risk being burned for heresy but if an object is really just a data structure, make the two variables public and lose the getters & setters. The regulars will know that I side mostly with Holub on "getters are evil" and all data should ideally be private, but let's not mechanically turn a lowly struct into a rich object.

More on topic, Kent Beck describes something called a "collecting argument" that is passed to one or more methods to collect all the bits it needs. Any of the methods could populate one or more attributes on the collector. So the thing described above could be an argument as well as a return type.


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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Another solution is to not have the method return anything, but introduce a Method Object - that is, have the method on its own class and let it set two instance variables, which's values you then can get via getters.

And there are probably a whole bunch of other options. To help you more in choosing one, we'd need to know more about why you have a method that wants to return two values.


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
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
the return-two-things idiom is common in dynamic scripting languages, in cases where strictly typed languages would return either an array, a collection, or a custom object. it's just that having dynamic typing and syntax for datatypes like tuples and lists as primitive types makes it convenient enough that nobody bothers creating custom classes for those custom objects.

consider what you might do in Java if there were clear, handy syntax for creating collections (lists or sets) of arbitrary objects, returning those collections from methods, then "unwrapping" them into their component parts on the other end. that's roughly what Wally was doing in the original example.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by M Beck:
it's just that having dynamic typing and syntax for datatypes like tuples and lists as primitive types makes it convenient enough that nobody bothers creating custom classes for those custom objects.


Which doesn't mean that not creating those classes is a good thing...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning Multiple Variables