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

Make sure you add a private String field to the User class named firstName...

I thought using

"this.firstName = firstName; " in User.java

makes the field private..

but ...I keep on getting this error message.

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

  public User(String firstName, String lastName) {
    // TODO: set and add the private fields

  this.firstName = firstName;
  this.lastName = lastName;
  }

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

}

[MOD: edited code out for brevity - srh]

1 Answer

Hi there,

At the top of the User class where the TODO comment is, you want to declare two private member variables that are of type String; the challenge tells you their names.

The addition of this inside the constructor doesn't make them private. And you can't assign anything to this.firstName unless the class has a member variable called firstName.

It would look like this:

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

Let me know how you get on.

Steve.