Write a Java Program to Call One Constructor from another
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Main { int sum; // first constructor Main() { // calling the second constructor this(5, 2); } // second constructor Main(int arg1, int arg2) { // add two value this.sum = arg1 + arg2; } void display() { System.out.println("Sum is: " + sum); } // main class public static void main(String[] args) { // call the first constructor Main obj = new Main(); // call display method obj.display(); } } |