English subtitles

← addFriend Method - Intro to Java Programming

Get Embed Code
7 Languages

Showing Revision 4 created 05/25/2016 by Udacity Robot.

  1. That might have been tricky. The answer should look something like this. There
  2. might be other ways to do it, but I think this is the simplest one. If you
  3. didn't get this, it's okay. There's more practice coming up. There are some
  4. subtletles to how this method works, so let's go through an example. Let's say
  5. we have two variables, one for Maria and one for Jamesha. Each of those should
  6. contain a person object with a name and friends. We have string objects. For
  7. Maria's name, Maria's current friends, Jamesha's name and Jamesha's current
  8. friends. Let's say we call maria.addFriend(jamesha). We call this method on the
  9. maria object and we pass in as an argument the Jamesha object. Now, when this
  10. line runs, the first thing that it does is creates a new string from these
  11. ones. This will be the new string that's created. It's going to put together
  12. friends, friend.name, and a space. Friends is the friends of the maria object
  13. because that's what we called the method on. So, we go to the Maria object, and
  14. friends is currently just TJ, with a space. friend.name is the friend, the
  15. argument, which was the Jamesha object, dot name, which is Jamesha. And the
  16. space is just a space. So, when these strings get put together, it'll look like
  17. TJ Jamesha. Now the string created by this part will be assigned into the
  18. friends variable. So, the old reference will go away and a new one will be
  19. created. And now, Jamesha appears in the friends of Maria. The old friends
  20. string that contained just TJ might still be around. And it hasn't changed, but
  21. the friends variable in the Maria object now contains a reference to a
  22. different string.