Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor with fewer arguments from a constructor

I have Constructor Tree(int a, int b, int c) and second Constructor Tree(int a, int b, int c, String s). How to load second constructor from first just to save writing all the logics? I thought about something like this but it gives me 'null' object.

public Tree(int a, int b, int c){
    Tree t1 = new Tree(a, b, c, "randomString");
}
like image 970
mike_hornbeck Avatar asked Dec 12 '22 23:12

mike_hornbeck


1 Answers

The magic word is this, e.g.

public Tree( int a, int b, int c, String d ) {
    // Do something
}

public Tree( int a, int b, int c ) {
    this( a, b, c, "randomString" );
}
like image 139
Rob Avatar answered Dec 23 '22 09:12

Rob