getInstance() in a Java bean

package nl.defrog.beans;
import nl.defrog.Utils;
public class UserBean implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final String BEAN_NAME = "User";
    private String username;
    public UserBean() {
    public String getUsername() {
        return username;
    public void setUsername(String username) {
        this.username = username;
    public static UserBean getInstance(String beanName) {
        return (UserBean) Utils.getVariableValue(beanName);
    public static UserBean getInstance() {
        return getInstance(BEAN_NAME);

package nl.defrog;

import javax.faces.context.FacesContext;

public class Utils {
	public static Object getVariableValue(String varName) {
		FacesContext context = FacesContext.getCurrentInstance();
		return context.getApplication().getVariableResolver().resolveVariable(context, varName);
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...