Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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.

Example:

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

Student s2 = new StudentBuilder()

                .name("Raman")

                .age(14)

                .motto("Hey, Mr Zee")

                .buildStudent();

Related questions

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

Browse Categories

...