title: @Named vs. @ManagedBean annotation
body: <p>I have read other posts on this but I still dont get it. I have this class:</p>
  
  <pre><code>import java.util.List;
  
  import javax.annotation.ManagedBean;
  import javax.annotation.PostConstruct;
  import javax.enterprise.context.RequestScoped;
  import javax.inject.Inject;
  
  @ManagedBean
  @RequestScoped
  public class ManagedBean{
  
      private UserService userService;
  
      @Inject
      public void setUserService(UserService userService) {
          this.userService = userService;
      }
  
      private User user;
      private List&lt;User&gt; list;
  
      public ManagedBean() {
      }
  
      @PostConstruct
      public void init() {
          user = userService.returnUserById(1);
          list = userService.returnAllUsers();
      }
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public List&lt;User&gt; getList() {
          return list;
      }
  
      public void setList(List&lt;User&gt; list) {
          this.list = list;
      }
  }
  </code></pre>
  
  <p>And then this class:</p>
  
  <pre><code>import java.io.Serializable;
  
  import javax.enterprise.context.SessionScoped;
  import javax.inject.Named;
  
  @Named
  @SessionScoped
  public class TestBean implements Serializable {
  
      private static final long serialVersionUID = 1L;
      private int num = 6;
      private String name;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getNum() {
          return num ;
      }
  
      public void setNum(int num) {
          this.num = num;
      }
  
  }
  </code></pre>
  
  <p>And then this .xhtml page:</p>
  
  <pre><code>&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"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"&gt;
  &lt;h:head /&gt;
  &lt;h:body&gt;
          TEST 
          Welcome #{testBean.num}
  
          &lt;h:form&gt;
          &lt;h:inputText value="#{testBean.name}"&gt;&lt;/h:inputText&gt;
          &lt;h:commandButton value="Welcome Me" action="xhtml/second"&gt;&lt;/h:commandButton&gt;
      &lt;/h:form&gt;
  
      &lt;h3&gt;Return All Users&lt;/h3&gt;
  
      &lt;h:dataTable value="#{homeManagedBean.list}" var="user"&gt;
          &lt;h:column&gt;
              &lt;h:outputText
                  value="User Id= #{user.userId} + User Name = #{user.userName}" /&gt;
          &lt;/h:column&gt;
      &lt;/h:dataTable&gt;
  
      &lt;h3&gt;Return User By Id 2&lt;/h3&gt;
  
      &lt;h:outputText value="User Id #{homeManagedBean.user.userId}" /&gt;
      &lt;h:outputText value="User Name #{homeManagedBean.user.userId}" /&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I change the @ManagedBean to @Named in the first class, I get the following error from glassfish:</p>
  
  <pre><code>WELD-000049 Unable to invoke public void a.ManagedBean.init() on a.ManagedBean@3b26b9ca
  </code></pre>
  
  <p>And when I change the @Named to @ManagedBean in the second class, I can call the page in the browser, but when I click on "Welcome me", I get the following error:</p>
  
  <pre><code>/index.xhtml @16,41 value="#{testBean.name}": Target Unreachable, identifier 'testBean' resolved to null
  </code></pre>
  
  <p>When do I use which annotation??</p>
  
  <p>Thanks for help!</p>