Java Program to add two numbers using class and object


In this program, You will learn how to add two numbers using class and object in Java.


20 = 10 + 10

40 = 10 + 30


Example: How to add two numbers using class and object in Java

Run it Now
import java.util.Scanner;

class Main {

    int sum(int a, int b) {
        return (a + b);
    }

    public static void main(String args[]) {

        int a, b, s;

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Two Numbers :");
        a = sc.nextInt();
        b = sc.nextInt();

        Main dd = new Main();
        s = dd.sum(a, b);

        System.out.println("\nThe Sum is :" + s);
    }
}

Output:

Enter Two Numbers :10 20
The Sum is :30