Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning more than one values from a method

 
jignesh soni
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In c and C++, it was possible to return more than one values from a method with the help of pointers. How can we return more than 1 values from a method in Java, as this is pass by value only and no pointers are used ?

Thanks
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jignesh soni:
In c and C++, it was possible to return more than one values from a method with the help of pointers. How can we return more than 1 values from a method in Java, as this is pass by value only and no pointers are used ?

Thanks


I think, it is very poor design to create classes so that a method can return more than one value. Java is Object Oriented, so design it that way. Your objects (and thereby classes) are more important than any of your methods within them. Maybe your function is trying to do too much, or else,what you really need is a collection/array.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this purpose, usually new classes are created. For instance, there is java.awt.Point for returning both the X and Y position when calling getLocation() of Components, and there is java.awt.Dimension for returning the width and height when calling getSize() of Components. java.awt.Rectangle is often used for similar purposes.

If the two values are completely unrelated, then it is indeed the question whether or not your design can be improved.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do the same trick as in C++ in Java, with arrays:

However, this is a "clever trick" that should not be necessary. It's better to create a class to hold the values.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any mutable object (which does not include String) that is passed into a method can be modified by the method.

However, having multiple side effects to a method is almost always a bad design.

You should first question your design, then use the above functionality. As mentioned above, this may be directing you to define a new type of object.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility is create an object that behaves like a pointer:


You can use getObject() and setObject() to derefence the "pointer".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic