Java optional parameters

后端 未结 17 1383
遇见更好的自我
遇见更好的自我 2020-11-22 13:19

How do I use optional parameters in Java? What specification supports optional parameters?

相关标签:
17条回答
  • 2020-11-22 14:15

    VarArgs and overloading have been mentioned. Another option is a Builder pattern, which would look something like this:

     MyObject my = new MyObjectBuilder().setParam1(value)
                                     .setParam3(otherValue)
                                     .setParam6(thirdValue)
                                     .build();
    

    Although that pattern would be most appropriate for when you need optional parameters in a constructor.

    0 讨论(0)
  • 2020-11-22 14:18

    This is an old question maybe even before actual Optional type was introduced but these days you can consider few things: - use method overloading - use Optional type which has advantage of avoiding passing NULLs around Optional type was introduced in Java 8 before it was usually used from third party lib such as Google's Guava. Using optional as parameters / arguments can be consider as over-usage as the main purpose was to use it as a return time.

    Ref: https://itcodehub.blogspot.com/2019/06/using-optional-type-in-java.html

    0 讨论(0)
  • 2020-11-22 14:19

    You can use a class that works much like a builder to contain your optional values like this.

    public class Options {
        private String someString = "default value";
        private int someInt= 0;
        public Options setSomeString(String someString) {
            this.someString = someString;
            return this;
        }
        public Options setSomeInt(int someInt) {
            this.someInt = someInt;
            return this;
        }
    }
    
    public static void foo(Consumer<Options> consumer) {
        Options options = new Options();
        consumer.accept(options);
        System.out.println("someString = " + options.someString + ", someInt = " + options.someInt);
    }
    

    Use like

    foo(o -> o.setSomeString("something").setSomeInt(5));
    

    Output is

    someString = something, someInt = 5
    

    To skip all the optional values you'd have to call it like foo(o -> {}); or if you prefer, you can create a second foo() method that doesn't take the optional parameters.

    Using this approach, you can specify optional values in any order without any ambiguity. You can also have parameters of different classes unlike with varargs. This approach would be even better if you can use annotations and code generation to create the Options class.

    0 讨论(0)
  • 2020-11-22 14:21

    You can do thing using method overloading like this.

     public void load(String name){ }
    
     public void load(String name,int age){}
    

    Also you can use @Nullable annotation

    public void load(@Nullable String name,int age){}
    

    simply pass null as first parameter.

    If you are passing same type variable you can use this

    public void load(String name...){}
    
    0 讨论(0)
  • 2020-11-22 14:21

    Overloading is fine, but if there's a lot of variables that needs default value, you will end up with :

    public void methodA(A arg1) {  }
    public void methodA( B arg2,) {  }
    public void methodA(C arg3) {  }
    public void methodA(A arg1, B arg2) {  }
    public void methodA(A arg1, C arg3) {  }
    public void methodA( B arg2, C arg3) {  }
    public void methodA(A arg1, B arg2, C arg3) {  }
    

    So I would suggest use the Variable Argument provided by Java. Here's a link for explanation.

    0 讨论(0)
提交回复
热议问题