Is it possible to return more than one value in java function? Like ,
public void retTest() {
return (30,40,50);
}
//From Caller fun
int a,b,c;
(a,b,c) = retTest();
Is it possible to return more than one value in java function? Like ,
public void retTest() {
return (30,40,50);
}
//From Caller fun
int a,b,c;
(a,b,c) = retTest();
No its not possible. But you can use return an object which encapsulates all your data.In that way coding,debugging and data extraction becomes easier as well as code enhancability is greatly increased..
such as :-
class Resut{
int a,b,c;
}
retTest()
{
return new Result();
}
You can do so by returning a List
of some kind (like an ArrayList
), or some other collection, or your own custom object that contains the fields, or an array.
For just a tuple result like that, an array would be a sensible option.
No you can't return many parameters in Java, even the out Keyword is not supported, you should create a new type that may contain your values and return it.
Class ReturnObject
{
//Fields you want to return here
public ReturnObjectConstructor(InterParamtersHere)
{
// Initialize your fields here
}
}
Public ReturnObject YourMethod()
{
ReturnObject returnObject = new ReturnObject(parameterHere)
return returnObject;
}
your method definition can be as follows
public List<Integer> retTest()
where you can return ArrayList
of integer values as follows
List<Integer> values = new ArrayList<Integer>() {
{
add(536);
add(85);
add(50);
add(45);
}
};
return values;