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

Function returning two reference variables

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But should i use a collection or just an object array would suffice for two objects
 
Campbell Ritchie
Sheriff
Pie
Posts: 49442
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use an object which encapsulates the two values, not an array or collection.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's already been answered, no? What else do you want to know?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12146
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

 
Sha Jar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic