dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
May 30, 2018

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Modifying Setters In Java

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()); 
Octavia Anghel
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date