• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning Multiple Variables

 
Wally Hartshorn
Ranch Hand
Posts: 77
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
M Beck
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):

 
Ben Wood
Ranch Hand
Posts: 342
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
M Beck
Ranch Hand
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic