Site icon TestingDocs.com

How to generate setter and getter automatically for a Bean?

Overview

JavaBeans are POJO ( Plain Old Java Objects ) classes that encapsulate objects into a single entity called Bean. They are serializable, have a zero-argument constructor. The main property of the Bean class is, it allows access to its properties by using getter and setter methods. These methods are called accessor and mutator methods respectively.

Conventions

The class must have a public default no-arg constructor and properties must be accessible using get, set methods.
The class should be serializable. This allows applications to store, and restore the bean’s state.

You can see that EmployeeBean has different properties and how to generate accessor methods automatically. The below tip saves a lot of time especially when you have more number of beans and each bean has lot of properties.

Eclipse Tip:

Once you write the variable names of the bean, go to

Source >> Generate Setters and Getters

Select the properties that you want setter/getter methods and complete the wizard.

The setter and getter code will be generated automatically.

 

EmployeeBean

public class EmployeeBean {
    String employeeName;
    int salary;
    String department;
    int bonus;
    String managerName;
    
    /**
     * @return the employeeName
     */
    public String getEmployeeName() {
        return employeeName;
    }
    /**
     * @param employeeName the employeeName to set
     */
    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }
    /**
     * @return the salary
     */
    public int getSalary() {
        return salary;
    }
    /**
     * @param salary the salary to set
     */
    public void setSalary(int salary) {
        this.salary = salary;
    }
    /**
     * @return the department
     */
    public String getDepartment() {
        return department;
    }
    /**
     * @param department the department to set
     */
    public void setDepartment(String department) {
        this.department = department;
    }
    /**
     * @return the bonus
     */
    public int getBonus() {
        return bonus;
    }
    /**
     * @param bonus the bonus to set
     */
    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
    /**
     * @return the managerName
     */
    public String getManagerName() {
        return managerName;
    }
    /**
     * @param managerName the managerName to set
     */
    public void setManagerName(String managerName) {
        this.managerName = managerName;
    }

}

 

Bean sample usage:

public class BeanUsage {

    public static void main(String[] args) {

        // Setters

        EmployeeBean eb = new EmployeeBean();
        eb.setEmployeeName("Surendra K");
        eb.setDepartment("Quality Assurance");
        eb.setSalary(10000);
        eb.setManagerName("Regan M");
        eb.setBonus(2400);

        // Getters

        System.out.println("*********Employee Record***************");
        System.out.println("Employee Name:" + eb.getEmployeeName());
        System.out.println("Manager Name:" +  eb.getManagerName());
        int monthlySalary = eb.getSalary() + eb.getBonus()/12 ;
        System.out.println("Salary:" + monthlySalary   );
    }

}

 

Exit mobile version