Java Platform, Enterprise Edition: The Java EE Tutorial

23.10 Adding Setter and Getter Methods

To make the state of the managed bean accessible, you need to 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:

package greetings;

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

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) { = name;

    public String getName() {
        return name;
Close Window

Table of Contents

Java Platform, Enterprise Edition: The Java EE Tutorial

Expand | Collapse