Why is the bean constructor called twice?

Why 'UserBean' is created twice? It happens when I visit the page first time. Next times the bean creates once. It has a default constructor with system out. So why does it happen?


    <h:form id="form">
        <p:panel id="panel">
            <h:panelGrid columns="3">
                <h:outputLabel for="email" value="E-mail: " />

                <p:inputText id="email"

                <h:outputLabel for="password" value="#{msg['_password']}: " />

                <p:inputText type="password" id="password"

            <p:commandButton id="btn" value="#{msg['_enter']}" update="panel"
                             action="#{userBean.login}" />


It has a default constructor with system out

That will happen when you're using a bean management framework which uses proxies, such as CDI (i.e. the bean is annotated with @Named, this is also confirmed by looking at your previous question). It'll during the first-time use create an instance in order to inspect the instance before creating a proxy. All subsequent instantiations are done through the proxy class.

You're at least not supposed to do anything in the bean constructor. Instead, use a method annotated with @PostConstruct if you intend to hook on managed bean initialization. And, use a method annotated with @PreDestroy if you intend to hook on managed bean destroyment.

public class Bean {

    public void init() {
        // ...

    public void destroy() {
        // ...


Method names are free to your choice. The above are just canonical names, taken over from several existing APIs, such as servlets and filters, hereby improving self-documentability of the code.

Need Your Help

using a string in an array

javascript arrays string

I have a string variable which I have defined as: