title: How to add an object to an ArrayList in Java
body: <p>I want to add an object to an <code>ArrayList</code>, but each time I add a new object to an <code>ArrayList</code> with 3 attributes: <code>objt(name, address, contact)</code>, I get an error.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Scanner;
  public class mainClass {
      public static void main(String args[]){
  
          Scanner input = new Scanner(System.in);
          System.out.println("Plz enter Name : ");
          String name = input.nextLine();
          System.out.println("Plz enter Address : ");
          String address = input.nextLine();
          System.out.println("Plz enter ContactNo : ");
          String contact = input.nextLine();
  
  
          ArrayList&lt;Data&gt; Contacts = new ArrayList&lt;Data&gt;();
          Data objt = new Data();
          Contacts.add(objt.Data(name, address, contact));
      }
  }
  </code></pre>
  
  <p>Here, <code>Data</code> is the class of which I'm trying to create an object and pass it to an <code>ArrayList</code>.</p>
  
  <pre><code>public class Data {
  
          private String name = "";
          private String address = "";
          private String cell = "";
  
  
          public void Data(String n, String a, String c){
  
              name = n;
              address = a;
              cell = c;
          }
          public void printData(){
  
              System.out.println("Name\tAddress\tContactNo");
              System.out.println(name + "\t" + address + "\t" + cell);
          }
  }
  </code></pre>