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 Objects Delivering the MVP Forum

John Grillo
John Grillo
30,241 Points

Java 'getFirstName' is written but quiz pretends it doesn't exist. What do?

I am currently trying to finish this Java quiz but for this part, the exercize seems to ignore the fact that I do have a 'getFirstName' method written. I have no idea what is going on and why it isn't working. Any insight would be appreciated.

Forum.java
public class Forum {
  private String topic;

  // TODO: add a constructor that accepts a topic and sets the private field topic
  public forum(String topic) {
    this.topic = topic;
  }
  public String getTopic() {
    return topic;
  }


  /* Uncomment this when you are prompted to do so
  public void addPost(ForumPost post) {
    System.out.printf("A new post in %s topic from %s %s about %s is available",
            topic,
            post.getAuthor().getFirstName(),
            post.getAuthor().getLastName(),
            post.getTitle()
    );
  }
  */

}
User.java
public class User {
  // TODO: add private fields for firstName and lastName
  private String firstName;
  private String lastName;

  public User(String firstName, String lastName) {
    // TODO: set and add the private fields
    firstName = this.firstname;
    lastname = this.lastName;

  }

  // TODO: add getters for firstName and lastName
  public String getFirstName() {
    return firstname;
  }

  public String getLastName() {
    return lastName;
  }

}
ForumPost.java
public class ForumPost {
  private User author;
  private String title;
  private String description;

  // TODO: add a constructor that accepts the author, title and description

  public User getAuthor() {
    return author;
  }

  public String getTitle() {
    return title;
  }

  public String getDescription() {
    return description;
  }
}
Main.java
public class Main {

  public static void main(String[] args) {
    System.out.println("Beginning forum example");
    if (args.length < 2) {
      System.out.println("Usage: java Main <first name> <last name>");
      System.err.println("<first name> and <last name> are required");
      System.exit(1);
    }
    /* Uncomment this when prompted
    Forum forum = new Forum("Java");
    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User();
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost();
    forum.addPost(post);

// */
  }

}

2 Answers

Hi John,

A few things here:

  1. Constructors are named after their class and are capitalised; your forum constructor has a lowercase f.
  2. Your User constructor is setting firstName = this.firstName - that needs to be the other way round; this.firstName = firstName;
  3. In your getFirstName method you are returning firstname not firstName - capitalise the 'N'

I hope that helps,

Steve.

John Grillo
John Grillo
30,241 Points

It does, thank you. why does this.firstName have to be on the right, like this.firstName = firstName; ?

You're setting the value of the member variable of the class, firstName to be equal to the value passed in as the argument, firstName. To distinguish the two, and a good IDE will do that for you, the member variable is identified with this to indicate that the variable you are setting is part of the instance you are working within. So, this.firstName is the member variable of the instance of the class you are working with; firstName is the received argument passed into the constructor. You set the former to equal the latter. The former holds no value yet; that's what the constructor is there to do; construct the instance.

Make sense?

Steve.

John Grillo
John Grillo
30,241 Points

oh, so this in this particular case refers to an instance of the class, not the narrower scope of the that method? Maybe I should watch that video again...if I could find it.

Yes. The keyword this is used with a class to indicate that the action is to take place on a member of this instance. If you get instances, you'll come to get this. Indeed, just carrying on and not dwelling on the detail is probably the best thing to do. It really does all become familiar in no time. Other languages; Swift and Python to name two that I know of; use self rather than this - it indicates ownership, uniqueness and entity better, in my view. Same gig, though. When dealing with class definitions, remember these are never used alone; they create instance/objects which are unique - the class itself is just a template - it never exists on its own. (There are exceptions to that - but let's worry about that later!!)