Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (2.6k points)

I came across some Java code that had the following composition:

public MyParameterizedFunction(String param1, int param2)


   this(param1, param2, false);


public MyParameterizedFunction(String param1, int param2, boolean param3)


   //use all three parameters here


I understand that in C++ I can specify a parameter a default value. For example:

void MyParameterizedFunction(String param1, int param2, bool param3=false);

Does Java recommend this kind of syntax? Are there any reasons why these two step syntax is better?

1 Answer

0 votes
by (46k points)

The answer is no, rather you should prefer to use the Build Pattern as it's used for constructors and is implemented by introducing a separate Builder class.


Student s1 = new StudentBuilder().name("Alia ").buildStudent();

Student s2 = new StudentBuilder()



                .motto("Hey, Mr Zee")


Related questions

+13 votes
9 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories