How to overload the constructor in Java

1 Answer

0 votes
public class Test   
{  
    int n;
    
    public Test(int val) {  
        n = val;  
    }  
    public Test(int a, int b) {  
        System.out.println(a + " " + b);
        n = a + b + 999;
    }  
 
    public static void main(String args[]) {
        Test ob1 = new Test(13);  
        Test ob2 = new Test(2, 5);  
        
        System.out.println(ob1.n);  
        System.out.println(ob2.n);  
    }
}


/*

run:

2 5
13
1006

*/

 



answered Sep 26, 2019 by avibootz
...