Can I put the default value in the parameter in Java?

Asked 2 years ago, Updated 2 years ago, 146 views

    public MyParameterizedFunction(String param1, int param2)
    {
        this(param1, param2, false);
    }

    public MyParameterizedFunction(String param1, int param2, boolean param3)
    {
        //use all three parameters here
    }

There's a code like this. In c++,

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

You can do this I thought it would work in Java

java default-value parameter method

2022-09-22 22:36

1 Answers

Not in Java. But if you use Build Pattern, you can do it similarly.

    Student s1 = new StudentBuilder().name("Eli").buildStudent();
    Student s2 = new StudentBuilder()
                     .name("Spicoli")
                     .age(16)
                     .motto("Aloha, Mr Hand")
                     .buildStudent();

This is how you use it. Please refer to it


2022-09-22 22:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.