Mohammad
0
Q:

java call another constructor

public class Foo {
    private int x;

    public Foo() {
        this(1);
    }

    public Foo(int x) {
        this.x = x;
    }
}
2
// Java program to illustrate Constructor Chaining 
// within same class Using this() keyword 
// and changing order of constructors 
class Temp 
{ 
    // default constructor 1 
    Temp() 
    { 
        System.out.println("default"); 
    } 
  
    // parameterized constructor 2 
    Temp(int x) 
    { 
        // invokes default constructor 
        this(); 
        System.out.println(x); 
    } 
  
    // parameterized constructor 3 
    Temp(int x, int y) 
    { 
        // invokes parameterized constructor 2 
        this(5); 
        System.out.println(x * y); 
    } 
  
    public static void main(String args[]) 
    { 
        // invokes parameterized constructor 3 
        new Temp(8, 10); 
    } 
} 
0

New to Communities?

Join the community