title: jsf2 ViewScoped bean initialization
body: <p>I'm new to jsf technology and I'm trying to understand when and how the <strong>ViewScoped</strong> jsf bean is initialized.</p>
  
  <p>I have a sample app with 2 beans</p>
  
  <h2>ApplicationScopedBean.java</h2>
  
  <pre class="lang-java prettyprint-override"><code>@ManagedBean
  @ApplicationScoped
  public class ApplicationScopedBean implements Serializable {
      private int incrementedCounter =0;
  
      public int getIncrementedCounter() {
          incrementedCounter += 1;
          return incrementedCounter;
      }
  }
  </code></pre>
  
  <h2>ViewScopedBean.java</h2>
  
  <pre class="lang-java prettyprint-override"><code>@ManagedBean
  @ViewScoped
  public class ViewScopedBean implements Serializable {
      @ManagedProperty(value = "#{applicationScopedBean}")
      private ApplicationScopedBean applicationScopedBean;
  
      private int reincarnationNumber;
  
      private int accessCounter;
  
      @PostConstruct
      public void initialize() {
          accessCounter = 0;
          reincarnationNumber = applicationScopedBean.getIncrementedCounter();
          System.out.println("Initializing viewScoped stateManager with reincarnationNumber value = " + String.valueOf(reincarnationNumber));
      }
  
      public void noAction() {
          //do nothing
      }
  
      public int getReincarnationNumber() {
          return reincarnationNumber;
      }
      public int getAccessCounter() {
          accessCounter += 1;
          return accessCounter;
      }
  
      public ApplicationScopedBean getApplicationScopedBean() {
          return applicationScopedBean;
      }
      public void setApplicationScopedBean(ApplicationScopedBean applicationScopedBean) {
          this.applicationScopedBean = applicationScopedBean;
      }
  }
  </code></pre>
  
  <p>ApplicationScoped bean is created only once for the application launch.
  Every time ViewScoped bean is being created, the <code>reincarnationNumber</code> is being increased by 1.</p>
  
  <p>I also have a simple jsf page to display these values:</p>
  
  <h2>index.xhtml</h2>
  
  <pre class="lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; 
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"&gt;
  
  &lt;h:head&gt;
      &lt;title&gt;View Scoped bean test&lt;/title&gt;
  &lt;/h:head&gt;
  
  &lt;h:body&gt;
      &lt;h:form&gt;    
          &lt;p&gt;ViewScoped bean reincarnation is &lt;h:outputText value="#{viewScopedBean.reincarnationNumber}"/&gt;&lt;/p&gt;
          &lt;p&gt;ViewScoped bean access counter is &lt;h:outputText value="#{viewScopedBean.accessCounter}"/&gt;&lt;/p&gt;
  
          &lt;h:commandButton type="submit" value="no action" action="#{viewScopedBean.noAction()}"/&gt;
          &lt;h:commandButton type="submit" value="reload this page" action="index"/&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <hr>
  
  <h2>The problem:</h2>
  
  <p>When I launch the application the first time, I already have <code>reincarnationNumber</code> value equal to <strong>3</strong>.</p>
  
  <p>In other words, I have this info displayed in browser:</p>
  
  <pre><code>ViewScoped bean reincarnation is 3
  ViewScoped bean access counter is 1
  </code></pre>
  
  <p>Why is that?
  <strong>Why ViewScoped bean has bean created 3 times?</strong></p>
  
  <p>Thanks in advance!</p>
  
  <h2><strong>SOLUTION</strong></h2>
  
  <p>As it is stated in the comments, the cause was "start browser" checkbox checked in Run Configurations in my IntelliJ IDEA. The trick is when browser is auto-started from IDE I have viewScopedBean initialized 3 times.</p>