dcsimg
Login | Register   
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


advertisement
 

Tip: Modifying Setters In Java

See why a Java bean, as a general rule, should not modify its data.


advertisement

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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()); 

 

Visit the DevX Tip Bank

 





   
Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies--XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date