Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Named Parameter idiom in Java

Tags:

java

idioms

How to implement Named Parameter idiom in Java? (especially for constructors)

I am looking for an Objective-C like syntax and not like the one used in JavaBeans.

A small code example would be fine.

like image 1000
Red Hyena Avatar asked Jan 01 '10 06:01

Red Hyena


People also ask

Why do we use named parameters?

Named arguments enable you to specify an argument for a parameter by matching the argument with its name rather than with its position in the parameter list. Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates.

How do you pass parameters to a name?

When you pass an argument by name, you specify the argument's declared name followed by a colon and an equal sign ( := ), followed by the argument value. You can supply named arguments in any order. When you call this procedure, you can supply the arguments by position, by name, or by using a mixture of both.

Can the argument have the same name as its parameter in Java?

Parameter Names This name is used within the method body to refer to the passed-in argument. The name of a parameter must be unique in its scope. It cannot be the same as the name of another parameter for the same method or constructor, and it cannot be the name of a local variable within the method or constructor.

What is parameter and argument in Java?

The term parameter refers to any declaration within the parentheses following the method/function name in a method/function declaration or definition; the term argument refers to any expression within the parentheses of a method/function call.


2 Answers

The best Java idiom I've seem for simulating keyword arguments in constructors is the Builder pattern, described in Effective Java 2nd Edition.

The basic idea is to have a Builder class that has setters (but usually not getters) for the different constructor parameters. There's also a build() method. The Builder class is often a (static) nested class of the class that it's used to build. The outer class's constructor is often private.

The end result looks something like:

public class Foo {   public static class Builder {     public Foo build() {       return new Foo(this);     }      public Builder setSize(int size) {       this.size = size;       return this;     }      public Builder setColor(Color color) {       this.color = color;       return this;     }      public Builder setName(String name) {       this.name = name;       return this;     }      // you can set defaults for these here     private int size;     private Color color;     private String name;   }    public static Builder builder() {       return new Builder();   }    private Foo(Builder builder) {     size = builder.size;     color = builder.color;     name = builder.name;   }    private final int size;   private final Color color;   private final String name;    // The rest of Foo goes here... } 

To create an instance of Foo you then write something like:

Foo foo = Foo.builder()     .setColor(red)     .setName("Fred")     .setSize(42)     .build(); 

The main caveats are:

  1. Setting up the pattern is pretty verbose (as you can see). Probably not worth it except for classes you plan on instantiating in many places.
  2. There's no compile-time checking that all of the parameters have been specified exactly once. You can add runtime checks, or you can use this only for optional parameters and make required parameters normal parameters to either Foo or the Builder's constructor. (People generally don't worry about the case where the same parameter is being set multiple times.)

You may also want to check out this blog post (not by me).

like image 57
Laurence Gonsalves Avatar answered Sep 28 '22 21:09

Laurence Gonsalves


This is worth of mentioning:

Foo foo = new Foo() {{     color = red;     name = "Fred";     size = 42; }}; 

the so called double-brace initializer. It is actually an anonymous class with instance initializer.

like image 41
irreputable Avatar answered Sep 28 '22 21:09

irreputable