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

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.