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 Data Structures Getting There Class Review

why return "mBody" over "body" in getBody?

I know my code is right but I do not understand why we cannot return "body" instead of "mBody." After all didn't we set them equal to each other in earlier lines?

com/example/BlogPost.java
package com.example;

import java.util.Date;

public class BlogPost { 
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;


public BlogPost(String author, String title, String body, String category, Date creationDate){
  mAuthor = author;
  mTitle = title; 
  mBody = body;
  mCategory = category;
  mCreationDate = creationDate;
 }
  public String getBody(){
    return mBody; 
  }
}
Mike Papamichail
Mike Papamichail
4,883 Points

Hello there!

You must return mBody since inside the constructor you assign the body's value to the mBody variable.

CAUTION: You are not setting them equal, you're just assigning the body's value-that was passed in during instantiation-to the mBody variable.And since mBody is a private field, other classes can't access it's value so we must create a getter method that returns it's value to the caller!

Hope this helps clarify a few things.Let me know what you think.

1 Answer

this helped alot, thank you!

Mike Papamichail
Mike Papamichail
4,883 Points

Awesome, if this solved your problem make sure to mark it as a "Best Answer" so that people with the same issue can easily find the solution!