How to Return 2 Values from a Java Method

How to return 2 values from a Java method?

Instead of returning an array that contains the two values or using a generic Pair class, consider creating a class that represents the result that you want to return, and return an instance of that class. Give the class a meaningful name. The benefits of this approach over using an array are type safety and it will make your program much easier to understand.

Note: A generic Pair class, as proposed in some of the other answers here, also gives you type safety, but doesn't convey what the result represents.

Example (which doesn't use really meaningful names):

final class MyResult {
private final int first;
private final int second;

public MyResult(int first, int second) {
this.first = first;
this.second = second;
}

public int getFirst() {
return first;
}

public int getSecond() {
return second;
}
}

// ...

public static MyResult something() {
int number1 = 1;
int number2 = 2;

return new MyResult(number1, number2);
}

public static void main(String[] args) {
MyResult result = something();
System.out.println(result.getFirst() + result.getSecond());
}

Can a method return two values in Java?

I will suggest to create a class "NumCounter"

public class NumCounter {

int positiveNumCounter;
int negativeNumCounter;
public int getPositiveNumCounter() {
return positiveNumCounter;
}
public void setPositiveNumCounter(int positiveNumCounter) {
this.positiveNumCounter = positiveNumCounter;
}
public int getNegativeNumCounter() {
return negativeNumCounter;
}
public void setNegativeNumCounter(int negativeNumCounter) {
this.negativeNumCounter = negativeNumCounter;
}
}

Then modify your method like

public static NumCounter positiveOrNegativeNumber(int n, NumCounter numCounter) {

if (n>0) {
numCounter.setPositiveNumCounter(numCounter.getPositiveNumCounter()+1);
} else if (n<0) {
numCounter.setNegativeNumCounter(numCounter.getNegativeNumCounter()+1);
}
return numCounter;
}

Return multiple value from a method

In order to return multiple values, you need to put them into one object and return that one. You could use a List or String[], but it would be more explicit to use a custom class for this pupose:

public static class Referee
{
private String name;
private String game1;
private String game2;

public Referee(String name, String game1, String game2)
{
this.name = name;
this.game1 = game1;
this.game2 = game2;
}

// Getters...
}

public static Referee getReferee(String referee_id, SQLiteDatabase sqLiteDatabase)
{
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM tbl_referee", null);
String refereeInfo = null;
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
String nameInRecord = cursor.getString(1);

if (nameInRecord.equals(referee_id))
{
String referee_name = cursor.getString(0); // 0 means table column of REFEREE NAME
String referee_game1 = cursor.getString(2); // 2 means table column of REFEREE GAME 1
String referee_game2 = cursor.getString(3); // 3 means table column of REFEREE GAME 2
return new Referee(referee_name, referee_game1, referee_game2);
}
} while (cursor.moveToNext());
}
}
return null;
}

If you have a method called getReferee, it is a good indicator that you also should have a type Referee that is returned by that method. This way, your code becomes self-explanatory.

You can use the new type in your main activity like this:

private void getRefereeProfile()
{
Referee memberInfo = SQLiteFunctionUtility.getReferee(referee_id, mDbHelper.getSqliteObjectWithReadable());
if (memberInfo != null)
{
txtview1.setText(memberInfo.getName());
txtview2.setText(memberInfo.getGame1());
txtview3.setText(memberInfo.getGame2());
// what is txtView4 for?
}
else
{
Toast.makeText(MainActivity.this, "No Referee Found", Toast.LENGTH_LONG).show();
}

Best practice for a Java method returning multiple values?

In general I'd go for the 4th or for a Map depending by the specific case, but if you need to return multiple unrelated values, I think that you have a serious design issue (Check https://en.wikipedia.org/wiki/Single_responsibility_principle)

In the specific case (after your comment), I'd definitely go with the 4th modeling the Response with all the required fields. Possibly you can also subtype with a ResponseSuccessful and ResponseFailure.



Related Topics



Leave a reply



Submit