Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Wrapping up

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,347 Points

How do I set the appropriate getters?

I'm not sure how I should set the appropriate gettters. I'm not sure if I have the private fields entered correctly either.

ForumPost.java
public class ForumPost {
  private User mAuthor;
  private String mTitle;
  private String mDescription;

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  // TODO: We need to expose the description
}
User.java
public class User {


  public User(String firstName, String lastName) {
    // TODO:  Set the private fields here
     private mFirstName = firstName;
  private mLastName = lastName;
  }

  public String getFirstName(){
    return firstName;
  }
  public String getLastName(){
    return lastName;
  }
}
Forum.java
public class Forum {
  private String mTopic;

  public Forum (String topic) {
    mTopic = topic;
  }

  public String getTopic() {
      return mTopic;
  }

  public void addPost(ForumPost post) {
      /* When all is ready uncomment this...
      System.out.printf("New post from %s %s about %s.\n",
                         post.getAuthor().getFirstName(),
                         post.getAuthor().getLastName(),
                         post.getTitle());
      */
  }
}
Example.java
public class Example {

  public static void main(String[] args) {
    System.out.println("Starting forum example...");
    if (args.length < 2) {
       System.out.println("first and last name are required. eg:  java Example Craig Dennis");
    }
    // Forum forum = new Forum("Java");
    // Take the first two elements passed args
    // User author = new User();
    // Add the author, title and description
    // ForumPost post = new ForumPost();
    // forum.addPost(post);
  }
}

1 Answer

Your getter are just a bit off. However, think about what are they returning. Check your constructor as well. What are you missing? How can you return something that doesn't exist? Make sure you read the error given in the "Preview" tab.

EDIT: a variable is valid only in it's scope. Therefore, variables from your constructor will not be accessible outside of it. How do you return them in the getters, considering what I just said?

Kyle Salisbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Salisbury
Full Stack JavaScript Techdegree Student 16,347 Points

public class User { private String mFirstName; private String mLastName;

public User(String firstName, String lastName) { // TODO: Set the private fields here mFirstName = firstName; mLastName = lastName; }

public String getFirstName (){ String firstName = " "; return firstName; }

public String getLastName () { String lastName = " "; return lastName; } }

That really helped. Thanks!

Just one more thing. Getters should only return the value in certain variables. Do not change it before that. You are not supposed to have these lines:

 String firstName = " ";
 String lastName = " ";