Sunday, July 12, 2009

Trouble with java objects for over a week?

Hi. I have two classes. One of them creates an array object. The other class uses this object to create its array.





This is the class that creates the array object;








class Album


{


String CDname;





public Album(String cd){





CDname= cd;


}











Then this object is used in this class here;








import javax.swing.*;





class FillAlbum


{


int count = 0;


Album[] Fill_Up= new Album[6];





String c = JOptionPane.showInputDialog("Please, enter CD name");





Fill_Up[count] = new Album(c);


count++;














Is there any way that once the Fill_Up array is filled up, that i can send this filled up array back to my Album class.


I tried creating a new object in my Album class that would get the instance from the Fill_Up array, but i had no joy.


Any help would be kool thanks

Trouble with java objects for over a week?
Ok,





First things first, you need to figure out WHY you would actually want to send the array BACK to the album object. The Album class seems to be designed to hold information for ONE album. Then you put each Album object you have into an Array (or Collection) of albums. I couldn't imagine a reason why one album would have to contain a collection of other albums. Also, you may want to look in to 'get' and 'set' methods for your Album objects so that you can change or return the name if you need to in your program.





Second, this 'FillAlbum' class seems like it would better suited as a method than an object. There's no constructor, and even though java gives you a default one you'll probably want to make your own so that you can set the array to a default size or let the user set their own. You may want to rethink making 'FillAlbum' (or fillAlbum if you wanted to use better naming conventions) a method within a class named 'AlbumList'. Let me try to show you what I mean:





public class AlbumList


{


final int DEFAULT_SIZE = 6; // Use a constant for default


int count; // Wait to initialize your attributes


Album[] data; // in the constructor





// Your Constructor(with no parameters)


public AlbumList()


{


data = new Album[DEFAULT_SIZE];


count = 0;


}





// Your Constructor(with a size parameter)


public AlbumList(int size)


{


data = new Album[size];


count = 0;


}





// Here's where your method to insert an album to the list


// I'll call it 'insert' because that's what it does, then we'll


// also make one that removes an album as well


public void insert( Album myAlbum )


{


data[count] = myAlbum;


count++;


}





// This remove method just takes out the last


// Album in the list. This list is starting to behave


// like a 'stack'. Look up some more info on that


// if you're interested


public void remove()


{


count--; // Simple, huh?


}





// Now, since we have added some of the foundations


// to your list we can get on to what you actually want


// to know, how to return the array. The 'Album[]' next


// to the method name lets the compiler know to return


// an array of Albums from this method.


public Album[] getList()


{


return data; // Also very simple


}


}





Then if you wanted to get the array from your AlbumList this is all you have to do:





AlbumList myList = new AlbumList();





// ...


// Some code that populates the list using insert


// ...





Album[] example = myList.getList();





Hopefully this will help and give you some design tips on how to properly create and use a Class. Good luck on your journey into Java.





-Mike
Reply:Yes, but you're making it more complicated than it needs to be. To stick with what you've already designed, you'll need to have each FillAlbum object be "long lived" so that when an Album[] reference is returned, it won't be garbage collected.





I recommend architecting your FillAlbum class with method(s) to accept an Album[] array as a method parameter, allocate the memory needed, and initialize each Album object. In this way, FillAlbum becomes more lightweight and can be considered a service class. In fact, you may even consider making your FillAlbum methods static if they're frequently used.





I can only guess what the function of your program is and how you plan to use each Album object. If you will be creating and destroying a bunch of these, I'd recommend you look into both the Factory pattern and potentially Object Pool (see links below). These techniques can both speed up your program execution and save your memory heap from constantly being garbage collected. I realize this is a little more than you asked about but thought you might appreciate the pointers...


No comments:

Post a Comment