type=page
status=published
title=Adding Setter and Getter Methods
next=cdi-basic011.html
prev=cdi-basic009.html
~~~~~~
= Adding Setter and Getter Methods


[[GJBBP]]

[[adding-setter-and-getter-methods]]
Adding Setter and Getter Methods
--------------------------------

To make the state of the managed bean accessible, add setter and getter methods for that state. The `createSalutation` method calls the bean's `greet` method, and the
`getSalutation` method retrieves the result.

Once the setter and getter methods have been added, the bean is
complete. The final code looks like this:

[source,oac_no_warn]
----
package greetings;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@RequestScoped
public class Printer {

    @Inject @Informal Greeting greeting;

    private String name;
    private String salutation;

    public void createSalutation() {
        this.salutation = greeting.greet(name);
    }

    public String getSalutation() {
        return salutation;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
----
