public class User { private String email; // Constructor and other methods public void setEmail(String email) { this.email = email; } }
import com.example.util.AuthUtil; public class User { private String email; // Constructor and other methods public void setEmail(String email) { boolean isValidEmail = AuthUtil.isValidEmail(email); if (isValidEmail) { this.email = email; } else { throw new IllegalArgumentException("Invalid email address"); } } }In this example, the User class checks if the given email parameter is valid or not using a utility class, AuthUtil, before setting the email field. If the email is not valid, an IllegalArgumentException is thrown. The package library for the setEmail method would depend on the context of the system where it is used. It could be a part of a custom user authentication library, an open-source framework, or a built-in Java library such as javax.mail or java.util.regex.