×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Modifying Setters In Java
 

Submitted by: Octavia Anghel(octaviaanghel@gmail.com)

Wednesday May 30th 2018 by Octavia Anghel

Language: Java, Expertise: Intermediate - See why, as a general rule, a bean should not modify its data, it is a data container, not business logic.

The code below eliminates spaces at the beginning or end of a name entered by the user, it tries to remove the spaces inside the setter method of a bean. It is not recommended that a bean modifies its data instead of simply holding it. The getter returns different data than that set by the setter.

private String name;

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

public void String getName() {
         return this.name;   
}

In general, a bean should not modify its data, it is a data container, not business logic. The trimming makes sense in the controller where the input occurs or in the logic where the spaces are not wanted.

person.setName(textInput.getText().trim()); 
Home
Mobile Site | Full Site
Copyright 2018 © QuinStreet Inc. All Rights Reserved