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

Alfredo Prince
Alfredo Prince
6,175 Points

Why do I get the error: non-static variable mTopic cannot be referenced from a static context mTopic=topic;

I can't identify the reason why I 'm getting this error.

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
  }
}
Forum.java
public class Forum {
  private String mTopic;

  public String getTopic() {
      return mTopic;
  }
    public static void forum (String topic){
      mTopic = topic;
    }
  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);
  }
}

1 Answer

In the "Forum" class, you need to remove the "static" keyword from the constructor:

public void Forum (String topic){
      mTopic = topic;
    }

"static" means that method isn't specific to an instance of the class. It can be used without creating an object of that class first. That means the method will do the exact same thing no matter with what "Forum" object you use it. For example:

public Class Hello {
public static void sayHello(String name){
      System.out.println("Hello "+name);
    }
}

That method will always say hello with the name you pass into it. It doesn't matter how many Hello objects you create, the method will always do the same thing. Obviously it doesn't make sense for a constructor, and of course a static method cannot set an instance variable since it's not specific to an instance. Also don't forget to type "Forum" and not "forum". Java is case sensitive and constructor must have the same name as the class.

Alfredo Prince
Alfredo Prince
6,175 Points

Yea I just figured it out. Thanks