Modifying Setters In Java

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) { = name.trim();   }public void String getName() {         return;   }

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.



Share the Post: