title: Using a un-named objects
body: <p>I'm learning Java and have the following question related to using objects in a List.</p>
  
  <p>I have created a class called <code>User</code>.</p>
  
  <p>This class uses a constructor which takes two strings when creating a new object:</p>
  
  <p><code>String username, String password</code></p>
  
  <p>I'm practicing creating Lists of User objects from the user class and have written the following:</p>
  
  <pre><code>    public void createAndManipulateListOfUsers(){
  
      List&lt;User&gt; userList = new ArrayList&lt;&gt;();
  
      userList.add(new User("user1","password1"));
      userList.add(new User("user2","password2"));
  </code></pre>
  
  <p>Now lets say I wanted to find the index position of one of these user objects using the <code>indexOf</code> method how would I do this since I've not named the objects?</p>
  
  <p>I've approached the same situation in a slightly different way, creating and naming each object before adding them to the list, like this:</p>
  
  <pre><code>    List&lt;User&gt; userList = new ArrayList&lt;&gt;();
  
      User bob = new User("user1","password1");
      User jim = new User("user2","password2");
  
      userlist.add(bob);
      userlist.add(jim);
  
      /* because i've given each object a name on creation 
      i am able to use the objects like this:*/
  
      System.out.println(userList.indexOf(bob));
      assertEquals(0,userList.indexOf(bob));
  </code></pre>
  
  <p>So is there a way that I can identify the <code>User</code> objects created in my first example in order to return the index positions using the <code>indexOf</code> method?</p>