function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alex TsitsuraAlex Tsitsura 

Variable number of arguments for a method?

Hi, 

It is possible to declare a method that will allow a variable number of parameters?

Java example:

void foo(String... args) {
    for (String arg : args) {
        System.out.println(arg);
    }
}
Sonam_SFDCSonam_SFDC
If I understand your requirement correctly - you are looking to pass multiple arguments of the same type in a function - 

For this you can use List<> to accept X number of arguments of the same type: https://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_collections_lists.htm
Alex TsitsuraAlex Tsitsura
Thanks, but i want call method as foo(1) or foo(1, 2), none foo(new List<Integer>{1, 2}).
Sonam_SFDCSonam_SFDC
Ahh got it.. so you want the number of Arguments to be a variable and they can be of different data types.
So in this case you can use List<Object> - this argument can accept any String/Account/Contact etc reference data type variable