How to return 2 values from a Java method


A method can give multiple values if we pass an object to the method and then modifies its values. See the example below −

Example

public class Tester {
   public static void main(String[] args) {
      Model model = new Model();
      model.data1 = 1;
      model.data2 = 2;
      System.out.println(model.data1 + ", " + model.data2);
      changeValues(model);
      System.out.println(model.data1 + ", " + model.data2);
   }
   public static void changeValues(Model model) {
      model.data1 = 100;
      model.data2 = 200;
   }
}
class Model {
   int data1;
   int data2;
}

Output

1, 2
100, 200

Updated on: 17-Jun-2020

564 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements