Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Wanted: How to return multiple values when using java.lang.reflection.invoke?

 
Ranch Hand
Posts: 420
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a little function that solves the following equation for real values only (as opposed to imaginary):

a*x^2 + b*x + c = 0;

My attempt is working, but I am not successfully passing back the values r1 and r2 to the calling function.

Is this possible in java? I thought everything except primitives were passed by reference in java!
Thanks
Siegfried
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't say what r1 and r2 are, and I'm not clear on what reflection has to do with anything, nor how pass-by-reference is related to this. This sounds like a case where you've built yourself up a nice big hairy mudball in your brain!

I can answer what I think is the root question here. A Java method can only return one value -- one primitive or one object reference. If you need to return two numbers from a method, then you need to bundle them up together somehow. Return them as a two-element array, or define a class to hold them and return an instance of that.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could create a special class that simply holds the two values:

An instance of that whis would be the object reference to return from your method.

[ September 28, 2006: Message edited by: Jean-Francois Briere ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW everything in Java is pass-by-value, primitives and object references.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!