We make a method named input which is used to get input from the user. We also make a method named add which is used to perform addition and return the result back to input method where we finally print the result.
Here is the source code of the Java Program to Return a Value from a Method. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Return_Value
int x, y, z;
Scanner s = new Scanner(System.in);
System.out.print("Enter first integer:");
x = s.nextInt();
System.out.print("Enter second integer:");
y = s.nextInt();
z = add(x, y);
int add(int a, int b)
c = a + b;
public static void main(String args)
Return_Value obj = new Return_Value();
$ javac Return_Value.java $ java Return_Value Enter first integer:5 Enter second integer:6 Result:11
Sanfoundry Global Education & Learning Series – 1000 Java Programs.