12

I am curious if APEX permits declaration of default arguments (or parameters if you will). When calling the method without specifying the value, method would be executed with the default value, otherwise with the value specified. I expect it would look somehow like this:

public String foo(String name, String greeting = 'Hello'){
    return greeting + ' ' + name;
}

Then calling this twice

foo('John');
foo('John', 'Good Evening');

would give me

Hello John
Good evening John

Is this somehow possible to do in APEX out of the box, or do I have to, for example, define overload for the method like this?

public String foo(String name){
    return foo(name, 'Hello');
}

public String foo(String name, String greeting){
    return greeting + ' ' + name;
}

2 Answers 2

19

In some use cases, you might want to consider fluent constructors on classes (or inner classes)

public class Foo {
  Integer bar = 0;  // default
  String  fie = 'Hello';

  public static Foo newInstance() {return new Foo();}
  public Foo withBar(Integer val) {this.bar = val; return this;} 
  public Foo withFie(String val) {this.fie= val; return this;}

  public doWork() {..}
}

and call via

Foo f = Foo.newInstance(); // everything defaults
f.doWork();

or

Foo f = Foo.newInstance()
          .withFie('goofball');  // let bar default
f.doWork();

or

SomeObject t = Foo.newInstance()
          .withBar(10)
          .doWork();  // let fie default and then do the method's work all in one statement

or

Foo f = Foo.newInstance()  // specify all args
          .withBar(10)
          .withFie('smushball');
f.doWork();

the pattern is handy for among other reasons, to self-document the args in the calling code though the pattern has greater use cases in applying repetitive and.or distinct operations to an object in a single statement, typically before transforming the object into something else.

2
  • I thought about mentioning the fluent pattern but didn't find time to fit it into my answer. Nice addition!
    – Adrian Larson
    Commented Feb 14, 2018 at 1:03
  • Seems like a lot of code gymnastics just to get default arguments! That's less of a knock on your code suggestion, and more of commentary on the language itself.
    – jbyrd
    Commented Jul 20, 2023 at 2:16
9

Yes, you have to define method overloads. That is the only way to specify default argument values.

4
  • 3
    Side note, I find it easier to read and work with if you put optional arguments at the end so the parameter order doesn't change when it gets shorter (your Apex example would use foo('John') and foo('Good Evening', 'John'), which is somewhat confusing).
    – Adrian Larson
    Commented Feb 13, 2018 at 16:21
  • Good point, I updated the code. Thx Commented Feb 13, 2018 at 17:00
  • 1
    Do NOT do this for @AuraEnabled methods, they will not work. But for methods called only from other Apex code, overrides with variant param lists is perfectly valid. Commented Sep 17, 2021 at 19:45
  • Yes I believe this constraint is explicitly mentioned in the docs.
    – Adrian Larson
    Commented Sep 17, 2021 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .