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 (Retired) Delivering the MVP Wrapping up

private Strings not initializing

The challenge asks me to initialize two private Strings and a comment inside the constructor tells me that the Strings should be initialized inside the constructor. But when I initialize the Strings inside the constructor, I receive error messages. Initializing them outside the constructor produces no error messages (although it is wrong). Is there a problem with my syntax or something?

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 String mFirstName;
  private String mLastName;
  }
}
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);
  }
}

Note that the problem is inside the User.java file.

4 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Ryan,

here a code suggestion for your problem:

public class User {
// declaring private String member variables of the class User should go here
        private String mFirstName;
        private String mLastName;

  public User(String firstName, String lastName) {
// this values assignment is called encapsulation in java
// should go inside the constructor
         mFirstName = firstName;
         mLastName = lastName;
  }
}

So if you create a new User object:

User newUser = new User("Ryan", "Moore");

The right part of the cosde above calls the constructor of the User class and your first and second names will be assigned to the member field variables mFirstName and mLastName.

I hope it helps

Grigorij

Athanasios Kourtzis
Athanasios Kourtzis
6,437 Points

Hi, in the User.java file the first curly closing brace should be before the private String member declarations. Which mean that they should be declared outside of your constructor. In your example there are inside the constructor which is wrong.

Hope this helps.

Cha Li
Cha Li
3,302 Points

the question want you to set string to some value. you first create the string outside the constructor private String mFirstName; private String mLastName; but right now they don't have any value next in constructor. public User(String firstName, String lastName) { // TODO: Set the private fields here mFirstName = firstName; mLastName = lastName // now they were initialize to the value you received which is parameter firstName and LastName

}

Thanks for everyone who responded! Yes, I was slightly misled because of the comment line:

// TODO:  Set the private fields here

I wrongly assumed that it meant I should put ALL the code there. Once I reviewed how my Workspace code was laid out, I realized that the variables had to be declared elsewhere and that "set" in the comment only meant "assign values to."