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


JavaRanch » Java Forums » Java » Java in General
Bookmark "Function returning two reference variables" Watch "Function returning two reference variables" New topic
Author

Function returning two reference variables

Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174



Please guide me if i should use array to return tow reference variables or inner class is better? Which is considered good coding skills?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Depends. If the returned types are two unrelated types then you'd have to return an Object[], which negates the value of type safety, IDE support, etc. I tend to use inner classes or utility classes instead--but that's just me.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Objects that manage pairs of items or values are quite
common. They are also easy to expand (extend) to handle
more items and mixed item types. So by using an object
you are on a very good track.
Jim ... ...


BEE MBA PMP SCJP-6
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
But should i use a collection or just an object array would suffice for two objects
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
I would use an object which encapsulates the two values, not an array or collection.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

If I'd really need a method to return two values, I'd write a generic class Pair, something like this:

And make a method which returns a Pair of the types that you want, for example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
What advantage this has over using Object array. I agree there is type checking and meaningful getters. But, for one time returning of two objects, which is preferable. I mean, i would like to know the pros and cons of each
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I think that's already been answered, no? What else do you want to know?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11351
    
  16

if you return an array, then your code will be sprinkled with

//or something like that - i may have the casting wrong

If you write a new class that encapsulates both A and B as member objects, you could write that containing class so that it knows what method to call...



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Sha Jar
Greenhorn

Joined: Mar 02, 2010
Posts: 23
Jacob Sonia wrote:[code]
Which is considered good coding skills?


The general rule is that a method should return a value of a specific type.
 
GeeCON Prague 2014
 
subject: Function returning two reference variables