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

Object References & Adding Videos to Courses Challenge

I thought I would post a little general, big-picture advice about the main thing that kept me confused for a while in a recent challenge:

All Java Objects are accessed by references.

Here's an example:

Object obj = new Object();
   // Creates a new object and then references it with the "obj" name.
Object other = obj;
   // How many objects are there now? Still 1.
   // It's just referenced by two names now - "obj" and "other".

Object another = new Object();
   // Now we have a new object referenced by "another" for a total of 2 objects.

I'm sure that was in a video somewhere, but I either missed it, forgot it, or never really understood the implications...

This means when Course.getVideos() returns a list of videos in that course, you don't have an identical list of videos - a copy of the list - you have an actual reference to the same list of videos. Anything you do to that changes the list in the Course object. (For a while I thought we were just creating a copy of the list and then not doing anything with it...)

This is the same for the other functions. The Map used in videosByTitle() should be created in that function so that there's a new map for each course passed in to the function. (At one point I was creating it in the class in the declaration of the variable at the top...)

Finally, this means that when you find the video in the Map, you have a reference to the actual video. Use the setTitle() method to change it's title.

I know Craig has been saying essentially the same thing in several comments, but I thought I'd take a minute to expound a little. Sometimes the language we use - like pull the video out of the map, add the video to the list, etc. is confusing. It's really about getting and putting references to the objects from maps and into lists, etc.