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

Jonas Troyer
Jonas Troyer
10,912 Points

Question about static context in Java.

I don't know why I'm still getting a static context in java. My methods seem to be not wanting to talk to each other to get back the post directly to the forum. It's giving me a error message that is telling me that static and non-static contexts can't be working together. I'm hoping that someone knows what I'm doing wrong so I can figure out this challenge.

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 getTopic) {
    this.topic = topic;

  }

  public String getTopic() {
    return topic;
  }



  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
      this.firstName = firstName;
      this.lastName = lastName;
  }

  public String getFirstName() {
    return this.firstName;
  }

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

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

  public ForumPost(User getAuthor, String getTitle, String getDescription) {
    this.author = author;
    this.title = title;
    this.description = 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 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);
    }

    Forum forum = new Forum("java");
    String firstName = args[0];
    String lastName = args[1];
    // TODO: pass in the first name and last name that are in the args parameter
    User author = new User(firstName, lastName);
    // TODO: initialize the forum post with the user created above and a title and description of your choice
    ForumPost post = new ForumPost(author, "Its a short post", "Thanks for being great and stuff");
    Forum.addPost(post);

  }

}

4 Answers

Joel Chester
Joel Chester
1,036 Points

Hey Jonas, I may be wrong on this but you may have to import the sub-classes to main.java.

Jonas Troyer
Jonas Troyer
10,912 Points

I'll check that and thank you.

Clinton Johnson
Clinton Johnson
28,714 Points
public class Forum {
  private String topic;
  private String name;
  // 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()
    );
  }
  */

}
Clinton Johnson
Clinton Johnson
28,714 Points

For the first challenge it just says to create/add a constructor for the Forum.java file. Please see how I created it and if you were correct except you just have to change the getTopic to topic and you will be fine. Jonas Troyer

Clinton Johnson
Clinton Johnson
28,714 Points
// For the 2nd Challenge here is the code below:
// 1. Create your private fields as below:
//2. Create your getters as below:

public class User {
  private String firstName;
  private String lastName;
  // 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;
  }

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

  public String getLastName() {
    return lastName;
  }
}
Clinton Johnson
Clinton Johnson
28,714 Points

Jonas Troyer Just remove the this (keyword) and you should be fine. If you have any questions please don't hesitate to post it and ill answer it.

Clinton Johnson
Clinton Johnson
28,714 Points
//For the 3rd Challenge again you are just creating a constructor and initializing it as follows:
// Any questions about the code please don't hesitate to ask of refer to videos for further understanding.

public class ForumPost {
  private User author;
  private String title;
  private String description;

  // TODO: add a constructor that accepts the author, title and description
  public ForumPost(User author, String title, String description) {
    this.author = author;
    this.title = title;
    this.description = description;
  }


  public User getAuthor() {
    return author;
  }

  public String getTitle() {
    return title;
  }

  public String getDescription() {
    return description;
  }
}