Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning multiple objects

 
George Nixon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably just a gap in my learning or something, but I'm a little confused. It seems that while it's easy to pass a lot of objects to a method as parameters, and change the values of each while the method runs, it's only possible (or, at least, relatively simple) to return the new value of one of objects.
For example,

How would I return all the objects, not just K? I suppose I could store the values inside a doSomeMath object with a public modifier, then get the values from the object one by one from the outside; it just seems there must be a more substantial way of doing the whole process.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How would I return all the objects, not just K? I suppose I could store the values inside a doSomeMath object with a public modifier, then get the values from the object one by one from the outside; it just seems there must be a more substantial way of doing the whole process.

First off, let's make sure that we understand the difference between a primative like an int and an Object. Objects must be insantiated with new, either directly or from some factory method. Primatives like int, long, double, etc. are just declared. Now for your problem. You could create a class and instantiate an object and return that as you mentioned and many times that is indeed a desireable solution. In the example you gave it would probably be much simpler to return an array of ints:

You could also use a Collection and wrap the ints in an object:

Michael Morris
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Mike said, in Java you read parameters in from the command line as an array, and typically you return multiple results from a method as an array. Works just fine. You just have to process the array on the receiving side.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
typically you return multiple results from a method as an array.
Errmm... I'd say typically you return multiple objects by creating a class which has members for each of the required objects. Michael alluded to this, but then for this particular problem an int[] array seemed simpler. However I think that most of the time, creating a class to hold the fields (or alternately, adding the fields to an existing class) is the way to go. Assuming that i, j, and k represent data with some significance in the real world and they actually have something to do with one another, chances are very good that a class which has fields for i, j, k will be useful - not just for returning the multiple values, but as a logical place to put all code which deals with the relationships between i, j, and k. It's difficult to see this with arbitrary made-up stuff like the i, j, k here - but if you look at a problem where the fields have meaning, it starts to make a lot more sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic