Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return value???

 
Suka Hati
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have 2 class....class A and class B....in class A i want to call a method in class B and send 2 parameter(v1 and v2).....in class B, it will transform v1 and v2 into v3 and v4.....the problem is...i need to send back the 2 new variables(v3 & v4) to class A....we can only return one value.....so, what i can do to send v3 & v4 back to class A???
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can return an array if the values are of the same type or you can send back an object that contains the two values as it's properties.
 
Suka Hati
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i send back an object does that means after that i can separate it into 2...???
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, create setter and getter methods for the two properties.

Set the two properties from within your method, return the object and then read the properties to get the values.
 
Suka Hati
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry...i cant really understand u....can u give me some example...
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If v1 and v2 are objects instead of primitives, the easiest solution is an Object array (or a specific array if they have the same class). Here's a trivial example:This will output the following.Does that clarify it? Ben's suggestion is to define a class with fields that will hold the returned objects along with a pair of get/set methods (accessors) for each. This class would satisfy the JavaBean requirements, so you might want to look that up at Sun's Java site if you need examples.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this isn't too hard to read.
I tried to make it, simple, terse, and readable, in a short amount of time.




[ February 22, 2005: Message edited by: Ben Souther ]
 
Suka Hati
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everybody....i manage to solve my problem....thanks to ben and david...i really appreciate the help from both of u...
 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii

Can we define a function in A class which can take 2 variables and call the function in B class so that v3 and v4 can be sent to class A ?

Thanks
Regards
M.Kartik
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kartik Mahadevan:
Hii

Can we define a function in A class which can take 2 variables and call the function in B class so that v3 and v4 can be sent to class A ?

Thanks
Regards
M.Kartik


Yes
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic