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.


class Main {

    //statement
   public static void main(String args[]) {
       Main dd = new Main();
    }
}

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

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("Sum is:" + s);
    }
}

Output:

Enter two numbers:10 20
Sum is:30