Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Arrays

Hi how can I include objects from a class into an array?

I have a class named Bunduk where I have a couple of family members as objects. I want to make an array string that stores these objects, these family members, which I have already defined in the class Bunduk. public class Bunduk { Bunduk omar = new Bunduk(); Bunduk khaled = new Bunduk(); Bunduk moaaz = new Bunduk(); } and so on .....

2 Answers

Hi Moaaz,

I think you are a little bit confused. The fact that you created a class named Bunduk and assign variables named after your family members to the newly instantiated objects doesn't mean that they represent your family members. Because those objects are empty, they do not hold any data in them, they don't represent much besides the fact that the name of the class matches your family surname (I suppose). That's one side of the problem. The second side is that Java is a statically typed language, meaning that you can't store apples in a bag that's design to store oranges. In your code, that translates to, you can not store objects of type Bundunk in an array of Strings because they are different types. That leaves you with two options: either you store Bundunks in an array of type Bundunk or if you want your array to be of type String, then you need to extract data from a Bundunk object that will return a String back to you.

I hope it was somewhat helpful.

thanks pedro this was helpful :D