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()); 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: