Constructors

The @BeanConstructor annotation is used to declare constructors as valid.

Because constructors initialize new objects, they cannot be considered as pure functions. The @BeanConstructor annotation allows you to declare constructors as valid so that they can be used in business rules and decision tables.

A constructor annotated with @BeanConstructor should meet the following requirements:
  • It should only initialize attribute values of the class.
  • The names of its parameters should match the names of some of the class attributes. It does not need to initialize all attributes.
  • A default constructor, which is one without parameters, can also be annotated with @BeanConstructor.

A constructor annotated with @BeanConstructor should preserve the relationship between parameter names and attribute names. Attribute naming follows the JavaBeans conventions for component properties.

By default, the Java™ compiler erases parameter names. To retain them, you can use one of the following options:
  • Compile with the -parameters option.
  • Annotate the constructor with @ConstructorAnnotations.
  • Annotate each parameter with @BusinessName.

The following example shows a @BeanConstructor that creates a new object Borrower and initializes its attribute values:

    @BeanConstructor
    public Borrower(String firstName,
                    String lastName,
                    Borrower spouse,
                    long yearlyIncome,
                    RiskLevel risk,
                    ZonedDateTime birthTime,
                    Address address) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.yearlyIncome = yearlyIncome;
        this.spouse = spouse;
        this.risk = risk;
        this.birthTime = birthTime;
        this.address = address;
    }