Prevent object stored in ArrayList from being over written
<p>Basically I have to write a simple contact manager, and store objects in array list.</p>

<p>What frustrates me is that I have newContact method, which when called creates new instance of Contact, and stores it in ArrayList. The problem is that every time I call that method, all other objets in the list gets overwritten.</p>

<pre><code>import java.util.ArrayList;
public class ContactManager {

  public static ArrayList&lt;Contact&gt; contactList = new ArrayList&lt;Contact&gt;();
  public static Contact[]c = {};

  public static void main(String[]args){
    newContact();
    newContact();

    System.out.println(contactList.get(1).getName());
    System.out.println(contactList.get(0).getName());
  }

  public static void newContact(){
    Contact c = new Contact();
    contactList.add(c);
  }
}
</code></pre>

<p>In Contact class constructore there is code that initializes the object's properties using Scanner class.</p>

<p>If in first call I Enter "John" and in second function call I enter "Peter", the above code will print out:</p>

<p>Peter
Peter.</p>

<p>Why doesn't it prints out John Peter?</p>

<p>Only thing I can think of is that maybe Java stores only reference to object in arraylist, and that unlike variables objects don't get destroyed after function executes..</p>

<p>Any ways around this?</p>

<p>I hope this explains what I am trying to achieve.</p>

<p>PS. I know people hate people that as homework questions. But I am doing this as an extra, in order to learn new stuff. Original assignment barely asks to instantiate 5 objects and store them in ArrayList. And I have that done, now I am just trying to see if I could come up with more dynamic solution. </p>

<p>Contact class code:</p>

<pre><code>import java.util.Scanner;
</code></pre>

<p>public class Contact{</p>

<pre><code>private static String name, number;

//constructor will ask to enter contact details
public Contact(){
    Scanner in = new Scanner(System.in);

    System.out.println("Enter name:");
    name = in.next();

    System.out.println("Enter number:");
    number = in.next();
}

//getters and setters

public static String getName(){
    return name;
}

public static String getNumber(){
    return number;
}

public static void setName(String newName){
    name = newName;
}

public static void setNumber(String newNumber){
    number = newNumber;
}

}
</code></pre>
