title: How do i add together values from all objects of a class in java?
body: <p>I am trying to retrieve certain values from multiple objects under the same class. I have used a for each loop to iterate through each object, and would like to create an aggregated total, representing the rating and the cost of the item from the objects.</p>
  
  <p>The For Each loop in my parent class:</p>
  
  <pre><code>for (Song songObj : Song.returnSongs()) {
      totalSongCost += Double.parseDouble(songObj.getPrice());
      totalSongRating += Integer.parseInt(songObj.getRating());
  }
  </code></pre>
  
  <p>The Child class ArrayList meant to store objects:</p>
  
  <pre><code>private int rating;
  private String title;
  private double price;
  private boolean favorite;
  private static int counter = 0;
  private static ArrayList songArray = new ArrayList();
  
  /**
   * Constructor for objects of class Song
   */
  public Song()
  {
      // initialise instance variables
      rating = 0;
      title = "";
      price = 0.0;
      counter++;
      songArray.add(this);
  }
  public static ArrayList returnSongs() {
      return songArray;
  }
  </code></pre>
  
  <p>When I compile the code I get an error message saying that an object cannot be converted to song. Is there a way to fix this, or an easier way to accomplish the same task?</p>