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 trialIvan Yudhi
2,031 Pointswhat's a 'getter'?
What's a 'getters method'? Is this some lingo generally used in developers world? Stuck in this exercise..
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
}
public class User {
private String mFirstName = "";
private String mLastName = "";
public User(String firstName, String lastName) {
// TODO: Set the private fields here
mFirstName = firstName;
mLastName = lastName;
}
}
public class Forum {
private String mTopic;
public String getTopic() {
return mTopic;
}
public 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());
*/
}
}
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);
}
}
2 Answers
lihaoquan
12,045 PointsFirstly, a 'Getter' is a method that GETS a variable for you. For example, you have a class called Shirt and you want to specify the shirts color, you might make a String variable called Color, then lets say maybe you want the shirt's color to be green, then you do Color = "Green";. Then maybe somewhere else in the program, you need to know the color of the Shirt, so you need a method to GET the shirt's color for you, like so : public String getColor() { return Color; }
lihaoquan
12,045 PointsIts just called a "Getter" because it GETS variables for you.
Jess Sanders
12,086 PointsFrom ForumPost.java:
public User getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
These are both examples of "getters" ---- For this part of the challenge, write "getters" in User.java and follow this format, returning mFirstName and mLastName
ISAIAH S
1,409 PointsISAIAH S
1,409 PointsWhat is the challenge asking?