This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Returning multiple objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Returning multiple objects" Watch "Returning multiple objects" New topic
Author

Returning multiple objects

George Nixon
Greenhorn

Joined: Mar 26, 2003
Posts: 1
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

Joined: Jan 30, 2002
Posts: 3451

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


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning multiple objects
 
Similar Threads
Three dim array
Unexpected GC Behavior
Garbage collection question
Java Code
suggestions on reading from file