Constructor with fewer arguments from the constructor

I have a constructor tree (int a, int b, int c) and a second constructor tree (int a, int b, int c, String s). How to load the second constructor the first time to save a record of all the logics? I was thinking about something like this, but it gives me an "null" object.

public Tree(int a, int b, int c){ Tree t1 = new Tree(a, b, c, "randomString"); } 
+4
source share
4 answers

This magic word, for example

 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" ); } 
+10
source
 public Tree(int a, int b, int c){ this(a, b, c, "randomString"); } 
+1
source

in the first line of the constructor, you can call another constructor:

 public Tree(int a, int b, int c, String s) { } public Tree(int a, int b, int c) { this(a,b,c,"someString"); } 
+1
source

You can simply call another constructor directly, using the this keyword to refer to the class containing this method. So you want:

 public Tree(int a, int b, int c){ this(a, b, c, "randomString"); } 
+1
source

Source: https://habr.com/ru/post/1305394/


All Articles