How to return 2 values from a Java method

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 06-Feb-2018 12:32:12
Advertisements