title: Override print method in Java
body: <p>I have a (syntax?) question about an overrided toString method and the get method for Array Lists. </p>
  
  <p>My first class titled BAccount creates an object that contains a name, balance, and account number. </p>
  
  <pre><code>public class BAccount { 
  private static int lastAssignedNumber=1000;
  String uName;
  double uBalance;
  private int accountNumber;
  public BAccount(String name, double balance) {
      uName=name;
      lastAssignedNumber++;
      uBalance=balance;
      accountNumber=lastAssignedNumber;
  }
  
  public void setName(String inputName) { 
      uName=inputName;
  }
  
  public void setBalance(double inputBalance) {
      uBalance=inputBalance;
  }
  
  public String getName() {
      return uName;
  }
  
  public double getBalance() {
      return uBalance;
  }
  
  public int getAccountNumber() {
      return accountNumber;
  }
  
  public String toString() { 
      return ("Account Holder Name: " + this.getName() +
              ", Acccount Balance: " + this.getBalance() +
              ", Account Number: " + this.getAccountNumber());
  }
  </code></pre>
  
  <p>My tester class creates 3 accounts,adds them to an ArrayList and prints each using the get method.</p>
  
  <pre><code>import java.util.*;
  public class Test { 
  public static void main(String[] args) { 
      Scanner in=new Scanner(System.in);
      ArrayList&lt;BAccount&gt; accountArray=new ArrayList&lt;&gt;();
      for(int i=0; i&lt;3; i++) { 
      System.out.print("Enter Account Holder Name: ");
      String name=in.nextLine();
      System.out.print("Enter Account Balance: ");
      double balance=in.nextDouble();
      in.nextLine();
      BAccount account= new BAccount(name, balance);
      accountArray.add(account);
      }
      System.out.println(accountArray.get(0));
      System.out.println(accountArray.get(1));
      System.out.println(accountArray.get(2));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The tester class creates 3 accounts,adds them to an ArrayList and prints each using the get method. </p>
  
  <p><strong>My question is</strong>: How does the ArrayList's get method know to use my  override toString method's format for printing?</p>