title: printing out attributes of multiple objects in java
body: <p>I have created a person class, with attributes age and name. I have created 5 instances of the class in an object array as well, and would like to print the attributes out with a for loop. It is my understanding that <code>toString</code> is explicitly called when you try to print out an object, so I override <code>toString</code> so that it prints out both attributes of the object when it is called.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>class Person
  {
      int age;
      String name;
  
      public Person(String name, int age)
      {
          this.age = age;
          this.name = name;
      }
  
  
      public String toString(Person p)
      {
          return "Name: " + p.name +"Age: "+ p.age;
      }
  }
  
  
  public class P1Q5Bubble {
  
  public static void main(String[] args) {
  
          Person [] pp = new Person [5];
  
          pp[0] = new Person("Andy" , 18);
          pp[1] = new Person("Lisa" , 20);
          pp[2] = new Person("Bob" , 10);
          pp[3] = new Person("Eva" , 18);
          pp[4] = new Person("Tim" , 13);       
  
  
  
  
          for(int i = 0; i&gt; pp.length ; i++)
              System.out.println(pp[i]);
  }
  </code></pre>