package examples; /** * CS401 Example CDTest * * This example demonstrates creating an array of a reference type in Java. * Unlike with primitive types, with reference types we need to make a new * object for each location in the array. Thus, we now have a three step * process to filling the array: * 1) Declare the array variable * 2) Creating the array object * 3) Creating each of the objects to be stored in the array * * @author Dr. Ramirez, PJ Dillon */ public class CDTest { public static void main (String [] args) { for (int i = 0; i < MusicCD.music_genres.length; i++) System.out.print(MusicCD.music_genres[i] + " "); System.out.println("\n"); MusicCD [] myCDs = new MusicCD[4]; myCDs[0] = new MusicCD("The Downward Spiral", "Nine Inch Nails", 8, "1/1/1994", 4, new String[] {""}, 3901); myCDs[1] = new MusicCD("Fables of the Reconstruction", "REM", 1, "", 11, new String[] {""},2379); myCDs[2] = new MusicCD("Diamond Life", "Sade", 4, "2/2/1985", 9, new String[] {""},2692); myCDs[3] = new MusicCD("Moby 18", "Moby", 0, "1/1/2002", 18, new String[] {""}, 4274); for (int i = 0; i < myCDs.length; i++) System.out.println(myCDs[i]); // Here we are changing a value in the static variable // music_types. Note how it is accessed through the class. Also // note that the only reason we can access it here is because it // is declared to be public. MusicCD.music_genres[1] = "Alternative"; System.out.println(myCDs[1]); } }