public static void main(String args[]) {
myMethod(); // i am calling static method from main()
}
.
public static ? myMethod(){
Finally i thought my way is better since when number of return types go higher this kind of a implementation do that in best way.
public static ReturningValues myMethod() {
ReturningValues rv = new ReturningValues();
rv.setValue("value");
rv.setIndex(12);
return rv;
}
the approach you took is good. Just Implementation may need to be better. For instance ReturningValues should be well defined and Its better if you can make ReturningValues as immutable.
// this approach is better
public static ReturningValues myMethod() {
ReturningValues rv = new ReturningValues("value", 12);
return rv;
}
public final class ReturningValues {
private final String value;
private final int index;
public ReturningValues(String value, int index) {
this.value = value;
this.index = index;
}
}
Or if you have lots of key value pairs you can use HashMap then
public static Map<String,Object> myMethod() {
Map<String,Object> map = new HashMap<String,Object>();
map.put(VALUE, "value");
map.put(INDEX, 12);
return Collections.unmodifiableMap(map); // try to use this
}
Generally if you are not sure of what value you will end up returning, you should consider using return-type as super-class of all the return values. In this case, where you need to return String or int, consider returning Object class(which is the base class of all the classes defined in java).
But be careful to have instanceof checks where you are calling this method. Or else you may end up getting ClassCastException.
public static void main(String args[]) {
Object obj = myMethod(); // i am calling static method from main() which return Object
if(obj instanceof String){
// Do something
}else(obj instance of Integer) {
//do something else
}
No. Java methods can only return one result (void
, a primitive, or an object), and creating a struct
-type class like this is exactly how you do it.
As a note, it is frequently possible to make classes like your ReturningValues
immutable like this:
public class ReturningValues {
public final String value;
public final int index;
public ReturningValues(String value, int index) {
this.value = value;
this.index = index;
}
}
This has the advantage that a ReturningValues
can be passed around, such as between threads, with no concerns about accidentally getting things out of sync.
The class you're looking for already exists. Map.Entry:
public static Entry<Integer,String> myMethod(){
return new SimpleEntry<>(12, "value");
}
And later:
Entry<Integer,String> valueAndIndex = myMethod();
int index = valueAndIndex.getKey();
String value = valueAndIndex.getValue();
It's just a simple two-field data structure that stores a key and value. If you need to do any special processing, store more than two fields, or have any other fringe case, you should make your own class, but otherwise, Map.Entry
is one of the more underutilized Java classes and is perfect for situations like these.
@ruchira ur solution it self is best.But i think if it is only about integer and a string we can do it in much easy and simple way..
class B {
public String myfun() {
int a=2; //Integer .. you could use scanner or pass parameters ..i have simply assigned
String b="hi"; //String
return Integer.toString(a)+","+b; //returnig string and int with "," in middle
}
}
class A {
public static void main(String args[]){
B obj=new B(); // obj of class B with myfun() method
String returned[]=obj.myfun().split(",");
//splitting integer and string values with "," and storing them in array
int b1=Integer.parseInt(returned[0]); //converting first value in array to integer.
System.out.println(returned[0]); //printing integer
System.out.println(returned[1]); //printing String
}
}
i hope it was useful.. :)