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 Java Data Structures Efficiency! Changing Course

Andrew Trachtman
Andrew Trachtman
3,680 Points

If Java is pass by value, I'm not sure how this solution works.

Because Java is pass by value, I'm confused as to how this code passed the challenge. I'm getting mVideo from the course object and I'm adding the video object I made to it. getvideos() returns a list mVideo, but isn't it just passing a copy of mVideo? When I modify the copy, how does it have any effect on the private variable from Course?

I'm confused. Any clarification would be appreciated.

 public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video video = new Video("The Beginning Bits");
    // TODO(2):  Add the newly created video to the course videos as the second video.
    course.getVideos().add(1, video);
  }
com/example/model/Course.java
package com.example.model;

import java.util.List;

public class Course {
  private String mName;
  private List<Video> mVideos; 

  public Course(String name, List<Video> videos) {
    mName = name;
    mVideos = videos;
  }

  public String getName() {
    return mName;
  }

  public List<Video> getVideos() {
    return mVideos;
  }

}
com/example/model/Video.java
package com.example.model;

public class Video {
  private String mTitle;

  public Video(String title) {
    mTitle = title;
  }

  public String getTitle() {
    return mTitle;
  }

  public void setTitle(String title) {
    mTitle = title;
  }

}
QuickFix.java
import com.example.model.Course;
import com.example.model.Video;

import java.util.Map;

public class QuickFix {

  public void addForgottenVideo(Course course) {
    // TODO(1):  Create a new video called "The Beginning Bits"
    Video video = new Video("The Beginning Bits");
    // TODO(2):  Add the newly created video to the course videos as the second video.
    course.getVideos().add(1, video);
  }

  public void fixVideoTitle(Course course, String oldTitle, String newTitle) {

  }

  public Map<String, Video> videosByTitle(Course course) {
    return null;
  }

}

1 Answer

Anders Björkland
Anders Björkland
7,481 Points

You are right to be confuced. Since we are often talking about reference variables in regards to objects, the difference between pass by value versus pass by reference can be obfuscated. But Java is pass by value. While you are not really passing the object itself, it's almost as if you did. You are passing a pointer that points to where that object recides. So when you change what is at the place where the pointer points, you do not need to return anything. The object has a life outside of the method. In Java you can't change where the pointer is pointing, but you can change what is being pointed at.

I found this discussion on Stack Overflow to be helpful.