aspose file tools*
The moose likes Beginning Java and the fly likes returning more than one values from a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "returning more than one values from a method" Watch "returning more than one values from a method" New topic
Author

returning more than one values from a method

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
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

Joined: Nov 28, 2007
Posts: 215

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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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.


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Another possibility is create an object that behaves like a pointer:


You can use getObject() and setObject() to derefence the "pointer".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: returning more than one values from a method