By gnavi


2009-06-15 18:04:28 8 Comments

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

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

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

I know that in C++ I can assign a parameter a default value. For example:

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

Does Java support this kind of syntax? Are there any reasons why this two step syntax is preferable?

20 comments

@simhumileco 2017-11-22 18:13:33

No, but the simplest way to implement this is:

public myParameterizedFunction(String param1, int param2, Boolean param3) {

    param3 = param3 == null ? false : param3;
}

public myParameterizedFunction(String param1, int param2) {

    this(param1, param2, false);
}

or instead of the ternary operator, you can use if:

public myParameterizedFunction(String param1, int param2, Boolean param3) {

    if (param3 == null) {
        param3 = false;
    }
}

public myParameterizedFunction(String param1, int param2) {

    this(param1, param2, false);
}

@AAGD 2019-03-12 12:17:20

I've now spent quite some time to figure out how to use this with methods that return values, and I haven't seen any examples so far, I thought it might be useful to add this here:

int foo(int a) {
    // do something with a
    return a;
}

int foo() {
    return foo(0); // here, 0 is a default value for a
}

@Vitalii Fedorenko 2013-11-01 02:03:37

There are several ways to simulate default parameters in Java:

  1. Method overloading.

    void foo(String a, Integer b) {
        //...
    }
    
    void foo(String a) {
        foo(a, 0); // here, 0 is a default value for b
    }
    
    foo("a", 2);
    foo("a");
    

    One of the limitations of this approach is that it doesn't work if you have two optional parameters of the same type and any of them can be omitted.

  2. Varargs.

    a) All optional parameters are of the same type:

    void foo(String a, Integer... b) {
        Integer b1 = b.length > 0 ? b[0] : 0;
        Integer b2 = b.length > 1 ? b[1] : 0;
        //...
    }
    
    foo("a");
    foo("a", 1, 2);
    

    b) Types of optional parameters may be different:

    void foo(String a, Object... b) {
        Integer b1 = 0;
        String b2 = "";
        if (b.length > 0) {
          if (!(b[0] instanceof Integer)) { 
              throw new IllegalArgumentException("...");
          }
          b1 = (Integer)b[0];
        }
        if (b.length > 1) {
            if (!(b[1] instanceof String)) { 
                throw new IllegalArgumentException("...");
            }
            b2 = (String)b[1];
            //...
        }
        //...
    }
    
    foo("a");
    foo("a", 1);
    foo("a", 1, "b2");
    

    The main drawback of this approach is that if optional parameters are of different types you lose static type checking. Furthermore, if each parameter has different meaning you need some way to distinguish them.

  3. Nulls. To address the limitations of the previous approaches you can allow null values and then analyse each parameter in a method body:

    void foo(String a, Integer b, Integer c) {
        b = b != null ? b : 0;
        c = c != null ? c : 0;
        //...
    }
    
    foo("a", null, 2);
    

    Now all arguments values must be provided, but the default ones may be null.

  4. Optional class. This approach is similar to nulls, but uses Java 8 Optional class for parameters that have a default value:

    void foo(String a, Optional<Integer> bOpt) {
        Integer b = bOpt.isPresent() ? bOpt.get() : 0;
        //...
    }
    
    foo("a", Optional.of(2));
    foo("a", Optional.<Integer>absent());
    

    Optional makes a method contract explicit for a caller, however, one may find such signature too verbose.

  5. Builder pattern. The builder pattern is used for constructors and is implemented by introducing a separate Builder class:

     class Foo {
         private final String a; 
         private final Integer b;
    
         Foo(String a, Integer b) {
           this.a = a;
           this.b = b;
         }
    
         //...
     }
    
     class FooBuilder {
       private String a = ""; 
       private Integer b = 0;
    
       FooBuilder setA(String a) {
         this.a = a;
         return this;
       }
    
       FooBuilder setB(Integer b) {
         this.b = b;
         return this;
       }
    
       Foo build() {
         return new Foo(a, b);
       }
     }
    
     Foo foo = new FooBuilder().setA("a").build();
    
  6. Maps. When the number of parameters is too large and for most of them default values are usually used, you can pass method arguments as a map of their names/values:

    void foo(Map<String, Object> parameters) {
        String a = ""; 
        Integer b = 0;
        if (parameters.containsKey("a")) { 
            if (!(parameters.get("a") instanceof Integer)) { 
                throw new IllegalArgumentException("...");
            }
            a = (String)parameters.get("a");
        } else if (parameters.containsKey("b")) { 
            //... 
        }
        //...
    }
    
    foo(ImmutableMap.<String, Object>of(
        "a", "a",
        "b", 2, 
        "d", "value")); 
    

Please note that you can combine any of these approaches to achieve a desirable result.

@Celeritas 2014-07-18 22:37:06

Good explanation. I've never seen return values used like this. For 5) what do the return this do? Also, doesn't FooBuilder().setA("a").build(); since (by definition) the constructor is called first and FooBuilder() returns a value, doesn't this mean .setA("a"): doesn't get the chance to be called?

@ADTC 2014-08-27 08:44:08

@Celeritas return this returns the same object on which the method was called (in the example, FooBuilder). This allows chaining of methods in one statement acting on the same object: new FooBuilder().setA(..).setB(..).setC(..) etc as opposed to calling each method in a separate statement.

@ADTC 2014-08-27 08:48:51

@Celeritas new FooBuilder() returns a FooBuilder object on which the setA method is called. As setB is not called, this.b retains the default value. Finally the build method is called on this FooBuilder object. The build method creates and returns a Foo object which is set to the variable Foo foo. Notice that the FooBuilder object is not stored in any variable.

@Martin Spamer 2015-01-27 09:47:59

Annotation can also be used to create default parameters and is most useful when required for polymorphic collections. docs.oracle.com/javase/tutorial/java/annotations/declaring.h‌​tml

@AdamMc331 2015-11-28 19:19:07

Over 900 upvotes on the same answer across two questions. I'm impressed: stackoverflow.com/questions/965690/java-optional-parameters/‌​…

@Pellet 2016-09-01 02:57:23

Java now supports optionals in 1.8, no need for guava :)

@Kolya Ivankov 2017-10-17 09:26:51

Thanks for the answer. It sad to see so many methods with not even one approaching C# in usability.

@Dr.jacky 2018-06-14 17:33:25

In case 6, why didn't use else if, instead of just multiple if?!

@mrts 2017-11-26 19:51:08

As Scala was mentioned, Kotlin is also worth mentioning. In Kotlin function parameters can have default values as well and they can even refer to other parameters:

fun read(b: Array<Byte>, off: Int = 0, len: Int = b.size) {
    ...
}

Like Scala, Kotlin runs on the JVM and can be easily integrated into existing Java projects.

@peterh 2014-11-04 09:12:10

No, but you can very easily emulate them. What in C++ was:

public: void myFunction(int a, int b=5, string c="test") { ... }

In Java, it will be an overloaded function:

public void myFunction(int a, int b, string c) { ... }

public void myFunction(int a, int b) {
    myFunction(a, b, "test");
}

public void myFunction(int a) {
    myFunction(a, 5);
}

Earlier was mentioned, that default parameters caused ambiguous cases in function overloading. That is simply not true, we can see in the case of the C++: yes, maybe it can create ambiguous cases, but these problem can be easily handled. It simply wasn't developed in Java, probably because the creators wanted a much simpler language as C++ was - if they had right, is another question. But most of us don't think he uses Java because of its simplicity.

@Kolya Ivankov 2017-10-17 09:27:43

The main idea of C# default value notation is precisely to avoid this boilerplate coding and have just one constructor instead of many.

@peterh 2017-10-17 09:34:18

@KolyaIvankov I don't know C#, but I know C++ where the reasoning is the same. I don't know what is the better, but I think, actually the same boilerplate code is generated by the compiler in the case of C++/C# and it is going into the final binary.

@Kolya Ivankov 2017-10-17 11:45:48

Every programming language is (in particular) a means to avoid an Assembler boilerplate, am I wrong? The question is only whether it gives a handy functionality, or not.

@peterh 2017-10-17 12:47:00

@KolyaIvankov I don't really understand your question. If you have a question, ask this as a question.

@Kolya Ivankov 2017-10-17 13:44:34

First sentence is a rhetorical question. The word "question" in the second sentence has nothing to do with the rhetorical question in the first.

@Kolya Ivankov 2017-10-17 13:50:05

Being more specific: languages are tools that allow us to write programs in a way we can have control over what is written, compiling is a way to tell a machine what we want from it. A tool is more useful if it allows us to avoid boilerplate. In effect, gnavi asked, if they can avoid precisely the type of boilerplate code you propose as an answer, since C# allows for it.

@Umair Khalid 2016-09-01 06:51:15

NO, But we have alternative in the form of function overloading.

called when no parameter passed

void operation(){

int a = 0;
int b = 0;

} 

called when "a" parameter was passed

void operation(int a){

int b = 0;
//code

} 

called when parameter b passed

void operation(int a , int b){
//code
} 

@Novaterata 2016-07-14 17:58:13

A similar approach to https://stackoverflow.com/a/13864910/2323964 that works in Java 8 is to use an interface with default getters. This will be more whitespace verbose, but is mockable, and it's great for when you have a bunch of instances where you actually want to draw attention to the parameters.

public class Foo() {
    public interface Parameters {
        String getRequired();
        default int getOptionalInt(){ return 23; }
        default String getOptionalString(){ return "Skidoo"; }
    }

    public Foo(Parameters parameters){
        //...
    }

    public static void baz() {
        final Foo foo = new Foo(new Person() {
            @Override public String getRequired(){ return "blahblahblah"; }
            @Override public int getOptionalInt(){ return 43; }
        });
    }
}

@Tomas Bjerre 2016-06-11 18:28:27

You may use Java Method Invocation Builder to automatically generate the builder with default values.

Just add @GenerateMethodInvocationBuilder to the class, or interface, and the @Default to parameters in methods where you want default values. A builder will be generated at compile time, using the default values that you specified with your annotations.

@GenerateMethodInvocationBuilder
public class CarService {
 public CarService() {
 }

 public String getCarsByFilter(//
   @Default("Color.BLUE") Color color, //
   @Default("new ProductionYear(2001)") ProductionYear productionYear,//
   @Default("Tomas") String owner//
 ) {
  return "Filtering... " + color + productionYear + owner;
 }
}

And then you can invoke the methods.

CarService instance = new CarService();
String carsByFilter = CarServiceGetCarsByFilterBuilder.getCarsByFilter()//
  .invoke(instance);

Or set any of the default values to something else.

CarService instance = new CarService();
String carsByFilter = CarServiceGetCarsByFilterBuilder.getCarsByFilter()//
  .withColor(Color.YELLOW)//
  .invoke(instance);

@Michael Sims 2015-01-18 15:40:19

This is how I did it ... it's not as convenient perhaps as having an 'optional argument' against your defined parameter, but it gets the job done:

public void postUserMessage(String s,boolean wipeClean)
{
    if(wipeClean)
    {
        userInformation.setText(s + "\n");
    }
    else
    {
        postUserMessage(s);
    }
}

public void postUserMessage(String s)
{
    userInformation.appendText(s + "\n");
}

Notice I can invoke the same method name with either just a string or I can invoke it with a string and a boolean value. In this case, setting wipeClean to true will replace all of the text in my TextArea with the provided string. Setting wipeClean to false or leaving it out all together simply appends the provided text to the TextArea.

Also notice I am not repeating code in the two methods, I am merely adding the functionality of being able to reset the TextArea by creating a new method with the same name only with the added boolean.

I actually think this is a little cleaner than if Java provided an 'optional argument' for our parameters since we would need to then code for default values etc. In this example, I don't need to worry about any of that. Yes, I have added yet another method to my class, but it's easier to read in the long run in my humble opinion.

@Hamzeh Soboh 2013-11-03 08:36:30

Try this solution:

public int getScore(int score, Integer... bonus)
{
    if(bonus.length > 0)
    {
        return score + bonus[0];
    }

    return score;
}

@ebelisle 2010-05-26 21:51:29

Unfortunately, yes.

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

could be written in Java 1.5 as:

void MyParameterizedFunction(String param1, int param2, Boolean... params) {
    assert params.length <= 1;
    bool param3 = params.length > 0 ? params[0].booleanValue() : false;
}

But whether or not you should depend on how you feel about the compiler generating a

new Boolean[]{}

for each call.

For multiple defaultable parameters:

void MyParameterizedFunction(String param1, int param2, bool param3=false, int param4=42) {}

could be written in Java 1.5 as:

void MyParameterizedFunction(String param1, int param2, Object... p) {
    int l = p.length;
    assert l <= 2;
    assert l < 1 || Boolean.class.isInstance(p[0]);
    assert l < 2 || Integer.class.isInstance(p[1]);
    bool param3 = l > 0 && p[0] != null ? ((Boolean)p[0]).booleanValue() : false;
    int param4 = l > 1 && p[1] != null ? ((Integer)p[1]).intValue() : 42;
}

This matches C++ syntax, which only allows defaulted parameters at the end of the parameter list.

Beyond syntax, there is a difference where this has run time type checking for passed defaultable parameters and C++ type checks them during compile.

@CurtainDog 2010-05-26 22:20:00

Clever, but varargs (...) can only be used for the final parameter, which is more limiting than what languages supporting default parameters give you.

@ebelisle 2010-05-27 02:52:34

I've added an example for multiple defaultable parameters.

@Someone Somewhere 2011-11-04 18:27:57

that's clever but a bit messy compared to the C++ version

@jwl 2012-02-23 23:31:30

Java definitely needs optional defaulted parameters as C# and others allow... the syntax is obvious and I presume they can implement this fairly simply even by just compiling all possible combinations... I cannot imagine why they haven't added it to the language yet!

@Michael Dorst 2013-10-01 18:52:22

One should never use an assert in production code. Throw an exception.

@Michael Dorst 2013-10-01 18:56:53

@joelarson maybe they're just trying to keep extra syntaxes to a minimum.

@Clashsoft 2015-02-21 18:36:31

Boolean != boolean... Boolean.class.isInstance(p[0]); can be written as p[0] instanceof Boolean...

@BrainSlugs83 2015-04-18 18:43:55

-1 This really isn't what varargs is for. This is a hack. -- in this case, using overloads will be way more readable (which is unfortunate, since three extra characters is more readable than 5 extra lines of source...). -- but Java does not support default parameters.

@volkovs 2016-04-21 15:06:27

That is so messy and error prone. I don't see any, any reason to use this instead of original way in the question (which author tried to simplify even further).

@Santosh Gokak 2009-06-15 18:16:49

No.

You can achieve the same behavior by passing an Object which has smart defaults. But again it depends what your case is at hand.

@Kathy Van Stone 2009-06-15 18:14:29

No, the structure you found is how Java handles it (that is, with overloading instead of default parameters).

For constructors, See Effective Java: Programming Language Guide's Item 1 tip (Consider static factory methods instead of constructors) if the overloading is getting complicated. For other methods, renaming some cases or using a parameter object can help. This is when you have enough complexity that differentiating is difficult. A definite case is where you have to differentiate using the order of parameters, not just number and type.

@user177800 2015-12-18 04:27:44

@Lii 2016-01-22 08:38:05

@JarrodRoberson: Static factory methods are no more harmful than new. They are used all the time in new code. Builders for simple value objects are often the result of over-engineering.

@Christian Aichinger 2016-01-27 21:55:16

@JarrodRoberson: Interesting way of forcing correct use via the compiler, thanks for sharing! Friendly suggestion for future posts: 300 lines of uncommented source code is probably a bit much to digest for most people (code is harder to read than to write, after all). Thanks again!

@user177800 2016-01-27 22:01:36

@ChristianAichinger - I have an even more strict advanced technique I am using now that combines generic interfaces with abstract classes with private constructors that I am working writing up as a blog post. My blog link is in my profile. as an aside, self documenting code does not need comments :)

@Christian Aichinger 2016-01-27 22:12:33

@JarrodRoberson: Nice, looking forward to it! What I wanted to communicate: as a reader of your blog, a 50 line example with a brief text description of what's going on would help me more than 300 lines without context.

@Levi Fuller 2018-12-18 03:47:07

@user177800 Disagree - static methods if written as pure functions are perfectly fine. It's when a static function mutates state that they become a problem...

@Moff 2019-05-08 07:15:10

@Lii I gave you the 100th upvote, you deserve it !

@IronWolf 2013-02-18 13:59:18

I might be stating the obvious here but why not simply implement the "default" parameter yourself?

public class Foo() {
        public void func(String s){
                func(s, true);
        }
        public void func(String s, boolean b){
                //your code here
        }
}

for the default you would ether use

func("my string");

and if you wouldn't like to use the default, you would use

func("my string", false);

@Offler 2013-02-18 14:10:57

The poster asked if this (rather ugly) pattern can be avoided ... ;-) In more modern languages (like c#, Scala) you don't need this extra overloads which only creates more lines of code. Up to some point you can use varargs in the meantime (static int max( int... array ) { }), but they are only a very ugly workaround.

@Antony Booth 2013-11-14 19:57:09

Overloading is not ugly and has many benefits, such as different method calls with different signatures can perform different functionality. //This is better public class Foo() { /* This does something */ public void func(String s){ //do something } /* This does something else with b */ public void func(String s, boolean b){ // b was passed } } //Than this public class Foo() { /* This does something unless b = value, then it does something else */ public void func(String s, boolean b = value){ If (b){ // Do Something } else{ // Do something else } } }

@Kapil 2014-01-20 04:47:41

Well, if one wants differing behaviour. If the only difference is a slight change in calculations, etc., it is sure a waste of effort to create multiple signatures. Defaults make sense where you need them to... and lack of it shouldn't be classified as a "useless" requirement.

@The incredible Jan 2017-04-06 13:09:23

@Offler default parameters have nothing to do with "modern language". I used them in Delphi 20 years ago and they probably already existed in Turbo Pascal.

@hoaz 2012-12-13 17:09:43

It is not supported but there are several options like using parameter object pattern with some syntax sugar:

public class Foo() {
    private static class ParameterObject {
        int param1 = 1;
        String param2 = "";
    }

    public static void main(String[] args) {
        new Foo().myMethod(new ParameterObject() {{ param1 = 10; param2 = "bar";}});
    }

    private void myMethod(ParameterObject po) {
    }
}

In this sample we construct ParameterObject with default values and override them in class instance initialization section { param1 = 10; param2 = "bar";}

@lythic 2011-07-21 16:26:05

You can do this is in Scala, which runs on the JVM and is compatible with Java programs. http://www.scala-lang.org/

i.e.

class Foo(var prime: Boolean = false, val rib: String)  {}

@om-nom-nom 2012-04-03 13:59:11

Bring whole new language to get one not so common feature?

@Val 2012-06-15 15:17:27

@om-nom-nom: Java should never existed. Saying that a feature is not used is equivalent that nobody needs it is saying that Java was not popular before it was invented means that Gosling should not start designing it.

@om-nom-nom 2012-06-15 15:22:29

@Val just saying that this is like shooting birds with cannons

@destan 2016-11-07 15:45:19

that has nothing to do with the OP's question

@Nicholas Jordan 2009-09-23 00:44:35

There are half a dozen or better issues such as this, eventually you arrive at the static factory pattern ... see the crypto api for that. Sort difficult to explain, but think of it this way: If you have a constructor, default or otherwise, the only way to propagate state beyond the curly braces is either to have a Boolean isValid; ( along with the null as default value v failed constructor ) or throw an exception which is never informative when getting it back from field users.

Code Correct be damned, I write thousand line constructors and do what I need. I find using isValid at object construction - in other words, two line constructors - but for some reason I am migrating to the static factory pattern. I just seems you can do a lot if you in a method call, there are still sync() issues but defaults can be 'substituted' better ( safer )

I think what we need to do here is address the issue of null as default value vis-a-vis something String one=new String(""); as a member variable, then doing a check for null before assigning string passed to the constructor.

Very remarkable the amount of raw, stratospheric computer science done in Java.

C++ and so on has vendor libs, yes. Java can outrun them on large scale servers due to it's massive toolbox. Study static initializer blocks, stay with us.

@Eli Courtwright 2009-06-15 19:20:11

No, but you can use the Builder Pattern, as described in this Stack Overflow answer.

As described in the linked answer, the Builder Pattern lets you write code like

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

in which some fields can have default values or otherwise be optional.

@nevvermind 2012-11-08 21:38:43

Finally e great less-than-2-pages example of the Builder pattern.

@ivanceras 2013-02-14 08:32:08

Im curious though, why do we need a builder class when using the builder pattern. I was thinking of Student s1 = new Student().name("Spicolo").age(16).motto("Aloha, Mr Hand);

@Eli Courtwright 2013-02-14 18:33:54

@ivanceras: It's relevant when classes have required fields, and you don't want to be able to instantiate those classes in an invalid state. So if you just said Student s1 = new Student().age(16); then that would leave you with a Student without a name, which might be bad. If it's not bad, then your solution is fine.

@Jules 2013-02-27 16:02:36

@ivanceras: another reason is you might want your class to be immutable after construction, so you wouldn't want methods in it that change its values.

@Abhijeet Kushe 2015-08-16 22:00:50

@ivanceras: I used Builders for 3 things - eliminating the multiple argument and fluent initialization, immutability and most importantly I feel to validate the domain object in the build() method.Why create an object instance if is invalid.You can also overload static factory methods like in the above case buildFreshman(), buildSenior() etc

@perreal 2017-04-18 05:24:38

this is exactly what default arguments are all about! amazing! you can also use the BFATV (be fully and totally verbose pattern)

@luis.espinal 2019-01-03 17:02:00

The use of the Builder pattern here seems rather superfluous for what the OP is asking (default parameter values.) As a matter of fact, I don't see how this provides an answer to the original question at all.

@luis.espinal 2019-01-03 22:02:25

Downvote all you want, this doesn't answer or address the original question.

@tomjen 2009-06-15 18:49:17

No. In general Java doesn't have much (any) syntactic sugar, since they tried to make a simple language.

@Martin 2011-09-18 12:46:23

Not quite. The bitter truth is that the team was on a tight schedule and had no time for syntactic sugar. Why else would const and goto be reserved keywords which no implementation? — Especially const is something I miss bitterly — final is no replacement and they knew it. — And if you made the concious decision to never implement goto you won't need to reserve the keyword. — And later in the Java Team cheated by making the Label based break and continue as powerful as a Pascal goto.

@mikera 2013-09-02 09:27:37

"Simple, Object-Oriented and Familiar" was indeed a design goal - see oracle.com/technetwork/java/intro-141325.html

@user2133061 2017-08-08 15:58:57

tomjen said: "No. In general Java doesn't have much (any) syntactic sugar, since they tried to make a simple language". So you are saying that removing a lot of unnecessary features from C++ makes a Java a simple language, then tell me why Java does have variadic methods? Why does it have varargs? It is not needed if you can simply use an array of objects instead, am I right? So varargs can be removed from the language, because it is unnecessary. This will make Java more simple than what it is now. Am I right? Overloading also can be removed, because you have infinite names for each method.

@matanster 2019-08-17 08:54:05

and that's why we got spring and stuff that take the simple language and turn any real Java project into a clusterfuck of syntax and boilerplate :-)

@Rob H 2009-06-15 18:05:16

Sadly, no.

@Trey 2009-06-15 19:46:24

Is it so sad? Doing so would introduce potentially ambiguous function signatures.

@AkiRoss 2010-01-21 11:42:04

I agree with Trey. This isn't sad :) and since default parameters exists since a long time, I guess Java engineers have some good reasons to don't include it ;)

@PhiLho 2011-05-24 13:09:32

@Trey: languages with default parameters often ditch function overloading since it is then less compelling. So no ambiguity. Beside, Scala added the feature in 2.8, and somehow solved the ambiguity issue (since they kept the overloading for compatibility reasons).

@Trey 2011-06-04 04:45:31

@PhiLho Yup, I love Scala but I don't mind lack of the feature in Java. It's really not that big of a deal that it is missing IMO. Scala allows them most of the time but will, in some circumstances at compile time, produce AmbiguousSignature errors (or something like that).

@Martin 2011-09-18 12:53:33

@Trey: read up how Ada implements overloading and default parameters and how Ada avoids ambiguity with named parameters. It take time to get the design right. Time the Java Team did not have. — PhiLho: Like Ada Scala has named parameter. A simple way to solve the problem. And they make the code better to read on top.

@Antony Booth 2013-11-14 18:58:24

I believe parameter defaults are bad practice and overloading is a much cleaner solution. You can even hide the default constructor and use static factory methods to return initialized instances. This is also useful for IOC, where you determine the implementation of the interface to return. If you want to optionally assign a property, create a public property, or overload a constructor Having another way to perform the same function does not make it better. It just makes the language more cryptic and is simply lazy programming when this is one of the reasons for function overloading.

@FlavorScape 2013-11-30 17:43:34

I fail to see how parameter defaults prevent function overloading. C# for instance allows overrides and also allows for default initializes. Seems like arbitrary choice, not restriction is the reason.

@user562566 2014-05-13 08:02:49

Yeah, lets trade off making the compiler do some extra work and instead make us all write 100000 overloads to give our library users convenience. Good idea.

@Trejkaz 2014-11-18 22:05:02

I agree that it's sad. When I'm writing a Java API for people to call from JRuby, I end up having to make every method take Map<?,?> and cram all parameters into the map so that idiomatic Ruby can be written. I'd much prefer parameter names and defaults because the Javadoc would look a lot nicer.

@spyro 2016-10-13 15:21:41

Works in C# for many years now without problems, even combined with method overloading. Compiler shows a warning on conflicts and that's it. Just do something like writeToFile(String content, boolea append=false);

@Mark K Cowan 2016-10-16 20:38:24

@user562566: Whenever I work on a Java project, I get the impression that Java devs are paid/measured by how many lines of code they produce per day

@javadba 2019-02-15 04:40:03

@MarkKCowan Do auto generated getters/setters "count" ?

@Mark K Cowan 2019-02-15 09:31:55

@javadba any lines in the source file, to a stupid enough manager. That's why java would be awesome in such companies, you'd get paid 3x more than the C# guy for writing the exact same program!

Related Questions

Sponsored Content

55 Answered Questions

[SOLVED] Creating a memory leak with Java

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

83 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

24 Answered Questions

[SOLVED] Set a default parameter value for a JavaScript function

47 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

31 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

26 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1852510 View
  • 2156 Score
  • 26 Answer
  • Tags:   java arrays

65 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3854375 View
  • 3307 Score
  • 65 Answer
  • Tags:   java random integer

26 Answered Questions

[SOLVED] How to get an enum value from a string value in Java?

  • 2009-03-02 22:56:34
  • Malachi
  • 1033409 View
  • 1860 Score
  • 26 Answer
  • Tags:   java enums

31 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

Sponsored Content